How to Parse this JSON Response in JAVA

后端 未结 2 1456
轻奢々
轻奢々 2020-12-06 06:55

I want to parse these kind of Json responses :

{
\"MyResponse\": {
    \"count\": 3,
    \"listTsm\": [{
        \"id\": \"b90c6218-73c8-30bd-b532-5ccf435da7         


        
2条回答
  •  日久生厌
    2020-12-06 07:49

    public static void main(String[] args) throws JSONException {
        String jsonString  = "{" + 
                "   \"MyResponse\": {" + 
                "       \"count\": 3," + 
                "       \"listTsm\": [{" + 
                "           \"id\": \"b90c6218-73c8-30bd-b532-5ccf435da766\"," + 
                "           \"simpleid\": 1," + 
                "           \"name\": \"vignesh1\"" + 
                "       }," + 
                "       {" + 
                "           \"id\": \"b90c6218-73c8-30bd-b532-5ccf435da766\"," + 
                "           \"simpleid\": 2," + 
                "           \"name\": \"vignesh2\"" + 
                "       }," + 
                "       {" + 
                "           \"id\": \"b90c6218-73c8-30bd-b532-5ccf435da766\"," + 
                "           \"simpleid\": 3," + 
                "           \"name\": \"vignesh3\"" + 
                "       }]" + 
                "   }" + 
                "}";
    
    
        JSONObject jsonObject = new JSONObject(jsonString);
        JSONObject myResponse = jsonObject.getJSONObject("MyResponse");
        JSONArray tsmresponse = (JSONArray) myResponse.get("listTsm");
    
        ArrayList list = new ArrayList();
    
        for(int i=0; i

    Output:

    [vignesh1, vignesh2, vignesh3]
    

    Comment: I didn't add validation

    [EDIT]

    other way to load json String

        JSONObject obj= new JSONObject();
        JSONObject jsonObject = obj.fromObject(jsonString);
        ....
    

提交回复
热议问题