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

时间:2016-06-09 07:10:15

标签: java json gson

需要将以下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:意外的char 84 at(行号= 1,列号= 16,偏移= 15)"

2 个答案:

答案 0 :(得分:0)

是这样的:

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

答案 1 :(得分:0)

解析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;

}