Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
字符串到Json对象到Java中的Json数组_Java_Json_Gson - Fatal编程技术网

字符串到Json对象到Java中的Json数组

字符串到Json对象到Java中的Json数组,java,json,gson,Java,Json,Gson,需要将下面的json响应转换为json对象,然后将“更新”字段转换为json数组。 基本上需要获取每个键、值对 { "Response": [ { "TicketID": "200", "update": [ { "Type": "Group", "user": "ABC", "groupName": "RTT",

需要将下面的json响应转换为json对象,然后将“更新”字段转换为json数组。 基本上需要获取每个键、值对

 {
"Response": [
    {
        "TicketID": "200",
        "update": [
            {
                "Type": "Group",
                "user": "ABC",
                "groupName": "RTT",
                "updatetime": "1465367505863"
            },
            {
                "Type": "status",
                "user": "ABC",
                "status": "Open",
                "updatetime": "1465367505858"
            },
            {
                "Type": "comment",
                "user": "ABC",
                "comments": "Updating ",
                "updatetime": "1465367505854"
            }
        ]
    },
    {
        "TicketID": "300",
        "update": [
            {
                "Type": "Group",
                "user": "ABC",
                "groupName": "RTT",
                "updatetime": "1465367505863"
            },
            {
                "Type": "status",
                "user": "ABC",
                "status": "Open",
                "updatetime": "1465367505854"
            },
            {
                "Type": "comment",
                "user": "ABC",
                "comments": "Updating Group",
                "updatetime": "1465367505834"
            }
        ]
    }
]
}
我尝试了以下代码:-

                    InputStream is = new ByteArrayInputStream(ticket.getBytes());
    // create json reader from json
    JsonReader reader = Json.createReader(is);

    // get the jsonobject structure from JsonReader
    JsonObject ticObj = reader.readObject();

    JsonArray arrTicket = ticObj.getJsonArray("update");
    String tktid=ticObj.getString("TicketID");
    System.out.println("Ticket id is "+tktid);

    System.out.println("ticket Id =>");

    for (javax.json.JsonValue value : arrTicket) {
        String ticketid = value.toString();
        System.out.print(" " + ticketid + "\n");

    }
将下面的错误获取为“javax.json.stream.JsonParsingException:Unexpected char 84 at(行号=1,列号=16,偏移量=15)”

如下操作:

try {
        JSONObject jsonObject = new JSONObject("{" +
                "\"Response\": [" +
                "    {" +
                "        \"TicketID\": \"200\"," +
                "        \"update\": [" +
                "            {" +
                "                \"Type\": \"Group\"," +
                "                \"user\": \"ABC\"," +
                "                \"groupName\": \"RTT\"," +
                "                \"updatetime\": \"1465367505863\"" +
                "            }," +
                "            {" +
                "                \"Type\": \"status\"," +
                "                \"user\": \"ABC\"," +
                "                \"status\": \"Open\"," +
                "                \"updatetime\": \"1465367505858\"" +
                "            }," +
                "            {" +
                "                \"Type\": \"comment\"," +
                "                \"user\": \"ABC\"," +
                "                \"comments\": \"Updating \"," +
                "                \"updatetime\": \"1465367505854\"" +
                "            }" +
                "        ]" +
                "    }," +
                "    {" +
                "        \"TicketID\": \"300\"," +
                "        \"update\": [" +
                "            {" +
                "                \"Type\": \"Group\"," +
                "                \"user\": \"ABC\"," +
                "                \"groupName\": \"RTT\"," +
                "                \"updatetime\": \"1465367505863\"" +
                "            }," +
                "            {" +
                "                \"Type\": \"status\"," +
                "                \"user\": \"ABC\"," +
                "                \"status\": \"Open\"," +
                "                \"updatetime\": \"1465367505854\"" +
                "            }," +
                "            {" +
                "                \"Type\": \"comment\"," +
                "                \"user\": \"ABC\"," +
                "                \"comments\": \"Updating Group\"," +
                "                \"updatetime\": \"1465367505834\"" +
                "            }" +
                "        ]" +
                "    }" +
                "]" +
                "}");

        JSONArray jsonArray = jsonObject.optJSONArray("Response");

        for (int i = 0; i < jsonArray.length(); i++) {

            JSONObject object = jsonArray.getJSONObject(i);

            String TicketID = object.optString("TicketID");

            System.out.println("TicketID ----"+TicketID);

            JSONArray update = object.getJSONArray("update");

            for (int j = 0; j < update.length(); j++) {

                JSONObject obj = update.getJSONObject(j);

                String Type = obj.optString("Type");
                System.out.println("Type :"+i+":"+Type);
                String user = obj.optString("user");
                System.out.println("user :"+i+":"+user);
                String groupName = obj.optString("groupName");
                System.out.println("groupName :"+i+":"+groupName);
                String updatetime = obj.optString("updattime");
                System.out.println("updatetime :"+i+":"+updatetime);
            }

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
试试看{
JSONObject JSONObject=新的JSONObject(“{”+
“\“响应\”:[”+
"    {" +
“\“TicketID\:\“200\”+
“\“更新\”:[”+
"            {" +
“\”类型\“:\”组\“+
“\”用户\“:\”ABC\“,”+
“\”组名\“:\”RTT\“,”+
““更新时间”:“1465367505863”+
"            }," +
"            {" +
“\”类型\“:\”状态\“+
“\”用户\“:\”ABC\“,”+
“状态”:“打开”+
““更新时间”:“1465367505858”+
"            }," +
"            {" +
“\”类型\“:\”注释\“+
“\”用户\“:\”ABC\“,”+
“\”注释\“:\”更新\“+
““更新时间”:“1465367505854”+
"            }" +
"        ]" +
"    }," +
"    {" +
“\'TicketID\:\'300\”+
“\“更新\”:[”+
"            {" +
“\”类型\“:\”组\“+
“\”用户\“:\”ABC\“,”+
“\”组名\“:\”RTT\“,”+
““更新时间”:“1465367505863”+
"            }," +
"            {" +
“\”类型\“:\”状态\“+
“\”用户\“:\”ABC\“,”+
“状态”:“打开”+
““更新时间”:“1465367505854”+
"            }," +
"            {" +
“\”类型\“:\”注释\“+
“\”用户\“:\”ABC\“,”+
“\”注释\“:\”更新组\“+
““更新时间”:“146536705834”+
"            }" +
"        ]" +
"    }" +
"]" +
"}");
JSONArray JSONArray=jsonObject.optJSONArray(“响应”);
for(int i=0;i
解析JSON响应并将它们添加到响应列表模型中,以后可以根据需要查询这些模型。由于响应不一致,请确保在每次解析响应时检查键,以避免崩溃

String response = "JSON Response String";

    ArrayList<ResponseModel> responseModelArrayList = new ArrayList<>();

    try {
        JSONObject rootJsonObject = new JSONObject(response);
        JSONArray responseJsonArray = new JSONArray(rootJsonObject.getString("Response"));

        for (int i = 0; i < responseJsonArray.length(); i++) {
            JSONObject childJsonObject = new JSONObject(responseJsonArray.get(i).toString());
            String str_ticketID = childJsonObject.getString("TicketID");
            JSONArray jsonArray = new JSONArray(childJsonObject.getString("update"));

            for (int j = 0; j < jsonArray.length(); j++) {
                JSONObject updateChildJsonObject = new JSONObject(jsonArray.get(j).toString());

                ResponseModel responseModel = new ResponseModel();
                responseModel.type = updateChildJsonObject.getString("Type");
                responseModel.user = updateChildJsonObject.getString("user");

                if (updateChildJsonObject.has("groupName"))
                    responseModel.groupName = updateChildJsonObject.getString("groupName");

                if (updateChildJsonObject.has("status"))
                    responseModel.status = updateChildJsonObject.getString("status");

                if (updateChildJsonObject.has("comments"))
                    responseModel.comments = updateChildJsonObject.getString("comments");

                responseModel.updateTime = updateChildJsonObject.getString("updatetime");

                responseModelArrayList.add(responseModel);
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

class ResponseModel implements Serializable {
    private String type;
    private String user;
    private String groupName;
    private String status;
    private String comments;
    private String updateTime;

}
String response=“JSON响应字符串”;
ArrayList responseModelArrayList=新建ArrayList();
试一试{
JSONObject rootJsonObject=新JSONObject(响应);
JSONArray responseJsonArray=新的JSONArray(rootJsonObject.getString(“Response”);
对于(int i=0;i
update
属于
响应
JSON O