简体   繁体   English

用Jackson解析没有标识符的JSON对象

[英]Parsing JSON Object with no identifier with Jackson

I am getting JSON from a web service, the JSON response I'm getting is: 我从Web服务获取JSON,我得到的JSON响应是:

{  
   "response":"itemList",
   "items":[  
      "0300300000",
      "0522400317",
      "1224200035",
      "1224200037",
      "1547409999"
   ]
}

I am looking to get each id within the items array. 我正在寻找项数组中的每个id。 The problem is I'm unsure how to parse this with Jackson when there are no identifiers for the id in the items array. 问题是当项目数组中没有id的标识符时,我不确定如何与Jackson解析。 My understanding is I could have an item class with a variable id and @JsonProperty ("id"), but I don't know how to proceed. 我的理解是我可以拥有一个带有变量id和@JsonProperty(“ id”)的项目类,但是我不知道如何进行。 I need to display these ids in a list (which I can do no problem once I have the data. 我需要在列表中显示这些ID(一旦有了数据,就可以毫无问题。

Could someone please point me in the right direction. 有人可以指出我正确的方向。

Thank you. 谢谢。

You could deserialize into something like 您可以反序列化为类似

public class MyData {
  public String response;
  public List<String> items;
}

(this will also work if you have private fields with public set methods). (如果您具有带有公共set方法的私有字段,这也将起作用)。 Or if you don't mind having jackson-specific annotations in your data classes, you can leave them as non-public and annotate them: 或者,如果您不介意在数据类中包含特定于杰克逊的注释,则可以将其保留为非公开并注释它们:

public class MyData {
  @JsonProperty
  String response;

  @JsonProperty
  List<String> items;
}

either way, use this to parse: 无论哪种方式,都可以使用它来解析:

import com.fasterxml.jackson.databind.ObjectMapper;
//...

MyData data=new ObjectMapper().readValue(jsonStringFromWebService, MyData.class);

I think so, you want this : 我想是的,您想要这样:

    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);
    }

You can Convert the JSON String to JSON object, and identify the array and get the IDs.. 您可以将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;
    }
}

and then: 接着:

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);
}

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

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