3

Im new to android all i have used previously is that i get JSON Array from server and parsed JSON objects like following,,

JSONObject objResponse = new JSONObject(retResult);
String objString = objResponse.toString();
String strResponse = null;
if ((objString.contains(getString(R.string.response)))
            || objString.contains((String) getString(R.string.response)
                    .toLowerCase())
            || (objString.contains((String) getString(R.string.response)
                    .toUpperCase()))) {
strResponse = objResponse.getString(getString(R.string.response));
} else {
    strResponse = "";
}

Help me to parse the following JSON return from server,,,

{
  "iapStandardHeight":[
    [
      53.4,
      65.3,
      71.6,
      76.2,
      80.2,
      83.9,
      87.3,
      90.5,
      93.6,
      95.7,
      98.3,
      100.8,
      103.1,
      107.3,
      111.2,
      115,
      118.7,
      125.9,
      129.2,
      132.3,
      135.6,
      138.9,
      142.1,
      145,
      147.7,
      150.4,
      153.2,
      156.3,
      159.8,
      163.4,
      167,
      170.6,
      174.1,
      177,
      179.1,
      180.6,
      181.5,
      182.1,
      182.6,
      182.9,
      183.1,
      183.3
    ],
    [
      51.2,
      62.8,
      69.1,
      73.5,
      77.4,
      80.9,
      84.1,
      87.1,
      89.9,
      91.8,
      94.2,
      96.5,
      98.6,
      102.5,
      106.2,
      109.6,
      113.1,
      118.5,
      121.6,
      124.6,
      127.7,
      130.8,
      133.8,
      136.6,
      139.1,
      141.6,
      144.2,
      147,
      150.2,
      153.4,
      156.7,
      160.1,
      163.6,
      166.7,
      169.2,
      171,
      172.3,
      173.1,
      173.7,
      174.2,
      174.5,
      174.7
    ],
    [
      49.9,
      61.4,
      67.6,
      72,
      75.7,
      79.1,
      82.3,
      85.1,
      87.8,
      89.6,
      91.9,
      94.1,
      96.1,
      99.9,
      103.3,
      106.7,
      110,
      114.9,
      118,
      120.9,
      123.9,
      126.9,
      129.8,
      132.4,
      134.9,
      137.2,
      139.7,
      142.4,
      145.3,
      148.3,
      151.5,
      154.8,
      158.2,
      161.4,
      164.1,
      166.1,
      167.5,
      168.5,
      169.2,
      169.7,
      170,
      170.4
    ],
    [
      48.6,
      60.1,
      66.2,
      70.5,
      74.1,
      77.4,
      80.4,
      83.2,
      85.8,
      87.4,
      89.6,
      91.7,
      93.6,
      97.2,
      100.5,
      103.7,
      106.8,
      111.4,
      114.4,
      117.2,
      120.1,
      123,
      125.7,
      128.3,
      130.6,
      132.9,
      135.2,
      137.7,
      140.5,
      143.3,
      146.2,
      149.4,
      152.8,
      156,
      158.9,
      161.1,
      162.7,
      163.8,
      164.6,
      165.1,
      165.5,
      165.9
    ],
    [
      46.3,
      57.6,
      63.6,
      67.7,
      71.3,
      74.4,
      77.2,
      79.7,
      82.1,
      83.5,
      85.5,
      87.4,
      89.1,
      92.4,
      95.4,
      98.4,
      101.2,
      104.7,
      107.5,
      110.1,
      112.7,
      115.4,
      117.9,
      120.1,
      122.2,
      124.3,
      126.3,
      128.4,
      130.8,
      133.1,
      135.6,
      138.5,
      141.7,
      145.1,
      148.2,
      150.8,
      152.8,
      154.1,
      155.1,
      155.8,
      156.3,
      156.7
    ]
  ],
  "iapStandardWeight":[
    [
      4.3,
      7.9,
      9.7,
      10.9,
      11.8,
      12.7,
      13.5,
      14.3,
      15.1,
      15.9,
      16.6,
      17.3,
      18,
      19.4,
      20.9,
      22.3,
      28.6,
      31.3,
      33.9,
      36.6,
      39.4,
      42.5,
      45.8,
      48.8,
      51.8,
      54.8,
      57.9,
      61.4,
      65.3,
      69.1,
      72.8,
      76.5,
      80.2,
      83.6,
      86.8,
      89.4,
      91.6,
      93.4,
      94.9,
      96.2,
      97.3,
      98.3
    ],
    [
      3.7,
      6.9,
      8.5,
      9.6,
      10.4,
      11.1,
      11.8,
      12.5,
      13.1,
      13.8,
      14.4,
      15,
      15.6,
      16.7,
      17.8,
      19,
      20.8,
      22.5,
      24.1,
      25.8,
      27.5,
      29.4,
      31.5,
      33.4,
      35.3,
      37.3,
      39.5,
      42.1,
      45,
      47.9,
      50.9,
      53.8,
      56.7,
      59.4,
      61.8,
      63.9,
      65.6,
      67,
      68.2,
      69.2,
      70.1,
      71
    ],
    [
      3.3,
      6.4,
      7.9,
      8.9,
      9.6,
      10.3,
      10.9,
      11.5,
      12.2,
      12.7,
      13.3,
      13.8,
      14.3,
      15.3,
      16.3,
      17.3,
      18.4,
      19.8,
      21.1,
      22.5,
      23.9,
      25.4,
      27,
      28.6,
      30.1,
      31.7,
      33.5,
      35.6,
      38,
      40.5,
      43,
      45.6,
      48.1,
      50.6,
      52.8,
      54.7,
      56.3,
      57.7,
      58.8,
      59.8,
      60.7,
      61.5
    ],
    [
      3,
      5.9,
      7.4,
      8.3,
      9,
      9.6,
      10.1,
      10.7,
      11.3,
      11.8,
      12.3,
      12.8,
      13.2,
      14.1,
      15,
      15.9,
      16.5,
      17.7,
      18.8,
      19.9,
      21.1,
      22.3,
      23.6,
      24.9,
      26.1,
      27.3,
      28.8,
      30.4,
      32.4,
      34.4,
      36.6,
      38.8,
      41,
      43.3,
      45.4,
      47.2,
      48.8,
      50.1,
      51.2,
      52.1,
      53.1,
      53.9
    ],
    [
      2.5,
      5.1,
      6.4,
      7.2,
      7.8,
      8.4,
      8.9,
      9.3,
      9.8,
      10.2,
      10.7,
      11.1,
      11.4,
      12.2,
      12.9,
      13.6,
      13.8,
      14.7,
      15.5,
      16.2,
      17,
      17.9,
      18.7,
      19.5,
      20.3,
      21.1,
      21.9,
      22.9,
      24.2,
      25.5,
      26.9,
      28.5,
      30.3,
      32.2,
      34.1,
      35.8,
      37.3,
      38.6,
      39.7,
      40.7,
      41.6,
      42.5
    ]
  ],
  "iapStandardAgeDiff":[
    "0.0",
    "0.3",
    "0.6",
    "0.9",
    "1.0",
    "1.3",
    "1.6",
    "1.9",
    "2.0",
    "2.3",
    "2.6",
    "2.9",
    "3.0",
    "3.6",
    "4.0",
    "4.6",
    "5.0",
    "6.0",
    "6.6",
    "7.0",
    "7.6",
    "8.0",
    "8.6",
    "9.0",
    "9.6",
    "10.0",
    "10.6",
    "11.0",
    "11.6",
    "12.0",
    "12.6",
    "13.0",
    "13.6",
    "14.0",
    "14.6",
    "15.0",
    "15.6",
    "16.0",
    "16.6",
    "17.0",
    "17.6",
    "18.0"
  ],
  "gender":"Boys",
  "dob":"2000/2/5",
  "IAPDetails":{
    "heightInCms":[
      152.401853206535
    ],
    "heightInFt":[
      5
    ],
    "heightInCmsDate":[
      "2014/1/25"
    ],
    "heightInFtDate":[
      "2014/1/25"
    ],
    "weightInKg":[
      45
    ],
    "weightInLbs":[
      99.20799000000001
    ],
    "weightInKgDate":[
      "2014/1/25"
    ],
    "weightInLbsDate":[
      "2014/1/25"
    ]
  },
  "response":"success"
}

help will be thankfull

2 Answers 2

3

The property iapStandardHeightlooks to be a jagged array. This looks to be the most complicated bit of the json you are receiving. Below is code for creating a jagged double array from the json you posted. If you need more help, just add a comment.

JSONObject jsonResponse = new JSONObject(response);

JSONArray outerArray = jsonResponse.getJSONArray("iapStandardHeight");
double[][] result = new double[outerArray.length()][];

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

    JSONArray innerArray = outerArray.getJSONArray(i);
    double[] innerResult = new double[innerArray.length()];

    for(int j = 0; j < innerArray.length(); j++){               
        innerResult[j] = innerArray.getDouble(j);               
    }

    result[i] = innerResult;
}
Sign up to request clarification or add additional context in comments.

Comments

2

try this

try {
                        JSONObject objResponse = new JSONObject(retResult);
                        JSONArray arr = objResponse.getJSONArray("iapStandardHeight");
                        for(int i=0; i<arr.length(); i++){
                            JSONArray arr2 = arr.getJSONArray(i);
                            for(int j=0; j<arr2.length(); j++){
                                double value = arr2.getDouble(j);
                            }
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.