简体   繁体   English

如何在Android中使用Jackson从JSONArray url获取java对象

[英]How to get java objects from JSONArray url using Jackson in Android

This is my JSON from URL https://api.myjson.com/bins/142jr 这是来自URL https://api.myjson.com/bins/142jr的我的JSON

[
  {
    "serviceNo":"SR0000000001",
    "serDate":"17",
    "serMonth":"DEC",
    "serYear":"2015",
    "serTime":"02.30 AM",
    "serApartmentName":"Galaxy Apartments"
  },
  {
    "serviceNo":"SR0000000002",
    "serDate":"19",
    "serMonth":"JUN",
    "serYear":"2016",
    "serTime":"03.30 AM",
    "serApartmentName":"The Great Apartments"
  }
]

I have one ListView I want populate details from online JSON,above i given a link and sample json anybody given sample jackson code in java 我有一个ListView我希望从在线JSON填充详细信息,上面给出了一个链接和示例json任何人给出了java中的示例jackson代码

Thanks for advance, Rajesh Rajendiran 谢谢你的进步,Rajesh Rajendiran

To use jackson you need to create a model class: 要使用jackson,您需要创建一个模型类:

[
  {
    "serviceNo":"SR0000000001",
    "serDate":"17",
    "serMonth":"DEC",
    "serYear":"2015",
    "serTime":"02.30 AM",
    "serApartmentName":"Galaxy Apartments"
  },
  {
    "serviceNo":"SR0000000002",
    "serDate":"19",
    "serMonth":"JUN",
    "serYear":"2016",
    "serTime":"03.30 AM",
    "serApartmentName":"The Great Apartments"
  }
]

For the above the json the model class would be: 对于上面的json,模型类将是:

public class SomeClass {
 private String serviceNo;
 private String serDate;
 private String serMonth;
 private String serYear;
 private String serTime;
 private String serApartmentName;

 @JsonProperty("serviceNo") //to bind it to serviceNo attribute of the json string
 public String getServiceNo() {
  return serviceNo;
 }

 public void setServiceNo(String sNo) { //@JsonProperty need not be specified again
  serviceNo = sNo;
 }

 //create getter setters like above for all the properties.
 //if you want to avoid a key-value from getting parsed use @JsonIgnore annotation

}

Now whenever you have the above json as string stored in a variable say jsonString use the following code to parse it: 现在每当你将上面的json作为存储在变量中的字符串说jsonString时,使用以下代码来解析它:

ObjectMapper mapper = new ObjectMapper(); // create once, reuse
ArrayList<SomeClass> results = mapper.readValue(jsonString,
   new TypeReference<ArrayList<ResultValue>>() { } );

results should now contain two SomeClass objects having the above json parsed as respective objects. 结果现在应该包含两个SomeClass对象,将上面的json解析为相应的对象。

PS: Its been a long time since I have used Jackson for parsing so this code might need some improvements. PS:自从我使用Jackson进行解析以来已经很长时间了,所以这段代码可能需要一些改进。

If you are getting this as http response then I would suggest to use spring rest template for android. 如果您将此作为http响应,那么我建议使用Spring rest模板的android。 It has support for Message Converters. 它支持消息转换器。 That way the onus of marshaling and unmarshalling. 这就是编组和解组的责任。

[Update] Here is a blog for the same : http://www.journaldev.com/2552/spring-restful-web-service-example-with-json-jackson-and-client-program [更新]这是一个相同的博客: http//www.journaldev.com/2552/spring-restful-web-service-example-with-json-jackson-and-client-program

Refer Docs for more details: 有关详细信息,请参阅文档:

http://docs.spring.io/spring-android/docs/current/reference/html/rest-template.html http://docs.spring.io/spring-android/docs/current/reference/html/rest-template.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM