Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Jackson解析没有标识符的JSON对象_Java_Json_Parsing_Jackson - Fatal编程技术网

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

Java 使用Jackson解析没有标识符的JSON对象,java,json,parsing,jackson,Java,Json,Parsing,Jackson,我从web服务获取JSON,得到的JSON响应是: { "response":"itemList", "items":[ "0300300000", "0522400317", "1224200035", "1224200037", "1547409999" ] } 我希望获得items数组中的每个id。问题是,当items数组中没有id的标识符时,我不确定如何用Jackson解析它。我的理解是我可以有一个带

我从web服务获取JSON,得到的JSON响应是:

{  
   "response":"itemList",
   "items":[  
      "0300300000",
      "0522400317",
      "1224200035",
      "1224200037",
      "1547409999"
   ]
}
我希望获得items数组中的每个id。问题是,当items数组中没有id的标识符时,我不确定如何用Jackson解析它。我的理解是我可以有一个带有变量id和@JsonProperty(“id”)的item类,但我不知道如何继续。我需要在一个列表中显示这些ID(一旦我有了数据,我就可以做到这一点)

谁能给我指一下正确的方向吗


谢谢。

您可以反序列化为

public class MyData {
  public String response;
  public 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);
    }
ArrayList notifArray=new ArrayList();
JSONObject jsonObj=新的JSONObject(结果行);
JSONArray jArray=jsonObj.getJSONArray(“项目”);
对于(inti=0;i
您可以将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));

感谢您的回复。对不起,我对反序列化或解析JSON不太熟悉,请您详细解释一下好吗?编辑后的答案看起来有点像我的答案,虽然原始答案离我很远;)添加到@JsonProperty中以允许正确映射对象,我试图编辑您的答案,但我无法, thanks@DJ-说得好,谢谢。事实上,您不一定需要jackson注释——如果您只是将字段公开或为其提供public
set
方法,jackson将使用这些方法,那么您的数据类就不需要了解jackson的任何信息(这可能是个问题,也可能不是个问题)——我已经更新了answer@CupawnTae啊,,我有我的私人…谢谢你的提示!最佳工具:@harcos nice工具,书签:-)
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);
}