使用 Jackson 解析 JSON 没有标识符的对象
Parsing JSON Object with no identifier with Jackson
我从网络服务中得到 JSON,我得到的 JSON 响应是:
{
"response":"itemList",
"items":[
"0300300000",
"0522400317",
"1224200035",
"1224200037",
"1547409999"
]
}
我想获取项目数组中的每个 ID。问题是当 items 数组中没有 id 的标识符时,我不确定如何用 Jackson 解析它。我的理解是我可以有一个带有变量 id 和 @JsonProperty ("id") 的项目 class,但我不知道如何继续。我需要在列表中显示这些 ID(一旦我有了数据,我就没问题了。
有人能给我指出正确的方向吗?
谢谢。
你可以反序列化成类似
的东西
public class MyData {
public String response;
public List<String> items;
}
(如果您的私有字段具有 public set
方法,这也适用)。或者,如果您不介意在您的数据 类 中使用 jackson 特定的注释,您可以将它们保留为非 public 并注释它们:
public class MyData {
@JsonProperty
String response;
@JsonProperty
List<String> items;
}
不管怎样,用这个来解析:
import com.fasterxml.jackson.databind.ObjectMapper;
//...
MyData data=new ObjectMapper().readValue(jsonStringFromWebService, MyData.class);
我想是的,你想要这个:
ArrayList<String> notifArray=new ArrayList<String>();
JSONObject jsonObj= new JSONObject (resultLine);
JSONArray jArray = jsonObj.getJSONArray("items");
for (int i = 0; i < jArray.length(); i++) {
String str = jArray.getString(i);
notifArray.add(str);
}
您可以将 JSON 字符串转换为 JSON 对象,并识别数组并获取 ID..
String josn = "{\"response\":\"itemList\", \"items\":[\"0300300000\",\"0522400317\",\"1224200035\",\"1224200037\",\"1547409999\"]}";
JSONObject jsonObject = new org.json.JSONObject(josn);
JSONArray itemsArray = jsonObject.getJSONArray("items");
System.out.println("Item - 1 =" + itemsArray.getString(0));
class Something {
public String response;
@JsonCreator
public Something(@JsonProperty("response") String response) {
this.response=response;
}
public List<String> items= new ArrayList<String>();
public List<String> addItem(String item) {
items.add(item);
return items;
}
}
然后:
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
String json = "{\"response\":\"itemList\",\"items\":[\"0300300000\",\"0522400317\"]}";
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(json, Something.class);
}
我从网络服务中得到 JSON,我得到的 JSON 响应是:
{
"response":"itemList",
"items":[
"0300300000",
"0522400317",
"1224200035",
"1224200037",
"1547409999"
]
}
我想获取项目数组中的每个 ID。问题是当 items 数组中没有 id 的标识符时,我不确定如何用 Jackson 解析它。我的理解是我可以有一个带有变量 id 和 @JsonProperty ("id") 的项目 class,但我不知道如何继续。我需要在列表中显示这些 ID(一旦我有了数据,我就没问题了。
有人能给我指出正确的方向吗?
谢谢。
你可以反序列化成类似
的东西public class MyData {
public String response;
public List<String> items;
}
(如果您的私有字段具有 public set
方法,这也适用)。或者,如果您不介意在您的数据 类 中使用 jackson 特定的注释,您可以将它们保留为非 public 并注释它们:
public class MyData {
@JsonProperty
String response;
@JsonProperty
List<String> items;
}
不管怎样,用这个来解析:
import com.fasterxml.jackson.databind.ObjectMapper;
//...
MyData data=new ObjectMapper().readValue(jsonStringFromWebService, MyData.class);
我想是的,你想要这个:
ArrayList<String> notifArray=new ArrayList<String>();
JSONObject jsonObj= new JSONObject (resultLine);
JSONArray jArray = jsonObj.getJSONArray("items");
for (int i = 0; i < jArray.length(); i++) {
String str = jArray.getString(i);
notifArray.add(str);
}
您可以将 JSON 字符串转换为 JSON 对象,并识别数组并获取 ID..
String josn = "{\"response\":\"itemList\", \"items\":[\"0300300000\",\"0522400317\",\"1224200035\",\"1224200037\",\"1547409999\"]}";
JSONObject jsonObject = new org.json.JSONObject(josn);
JSONArray itemsArray = jsonObject.getJSONArray("items");
System.out.println("Item - 1 =" + itemsArray.getString(0));
class Something {
public String response;
@JsonCreator
public Something(@JsonProperty("response") String response) {
this.response=response;
}
public List<String> items= new ArrayList<String>();
public List<String> addItem(String item) {
items.add(item);
return items;
}
}
然后:
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
String json = "{\"response\":\"itemList\",\"items\":[\"0300300000\",\"0522400317\"]}";
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(json, Something.class);
}