2

Possible Duplicate:
Sending and Parsing JSON in Android

I have a JSON result in the following format which JSON Lint shows this as a Valid Response.

My question is: how do I accesss the content of "reportId0" value "164", "reportId1" value 157,reportId2 value 165, etc are all dynamic values?

My sample code for accessing value of properties.How to get Value reportid And add allvalue in Arraylist?

"properties": {    
    "link": "",
    "approvalsReportCount": 3,
    "reportName0": "srcapprovals",
    "reportId0": 164,
    "reportName1": "Approvals",
    "reportId1": 157,
    "requests_report_id": "163",
    "requests_report_name": "EG approvals",
    "reportName2": "fulfillment",
    "reportId2": 165
}
Community
  • 1
  • 1
Pankit
  • 163
  • 9

4 Answers4

3

This is the best way i found it to get ReportId value.

Below is My code

JSONObject jObj = new JSONObject(result);
            JSONObject jsonResultArray = jObj.getJSONObject("results");
            JSONObject pro_object = jsonResultArray.getJSONObject("properties");
            Iterator keys = pro_object.keys();
           while(keys.hasNext()) {
                String currentDynamicKey = (String)keys.next();
                String value = pro_object.getString(currentDynamicKey);
                String upToEightCharacters = currentDynamicKey.substring(0, Math.min(currentDynamicKey.length(), 8));

                if(upToEightCharacters.startsWith("reportId"))
                {

                    Log.v("key"," new report ID key  " + currentDynamicKey);
                    Log.v("key"," new report ID key  " + pro_object.getString(currentDynamicKey) );

                }
              }
jigspatel
  • 400
  • 3
  • 14
2

you can use this

public ArrayList<String> getReportIds() {
    boolean isContinue = true;
    JSONObject json;
    String tag = "reportId";
    int i = 0;
    ArrayList<String> repIdList = new ArrayList<String>();
    JSONObject prop = null;
    try {
        json = new JSONObject("<your json string>");
        prop = json.getJSONObject("properties");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    while (isContinue) {
        String repId = "";
        try {
            repId = prop.getString(tag + i);
            repIdList.add(repId);
            i++;
        } catch (JSONException e) {
            isContinue = false;
            e.printStackTrace();
        }
    }
    return repIdList;
}
Jignesh Ansodariya
  • 12,583
  • 24
  • 81
  • 113
1

You can Try This!!

try {
               JSONObject jObj = new JSONObject(result);
               JSONObject jsonResultArray = jObj.getJSONObject("results");
               Log.v("log_tag","json result Array :   "+ jsonResultArray);

               JSONObject pro_object = jsonResultArray.getJSONObject("properties");

               Iterator keys = pro_object.keys();

                 while(keys.hasNext()) {
                      // loop to get the dynamic key
                      String currentDynamicKey = (String)keys.next();
                      String value = pro_object.getString(currentDynamicKey);
                      approvaldto_Key = new All_Approval_Key_dto();

                      String upToEightCharacters = currentDynamicKey.substring(0, Math.min(currentDynamicKey.length(), 8));

                      if(upToEightCharacters.startsWith("reportId"))
                      {

                          approvaldto_Key.requestId = pro_object.getString(currentDynamicKey);
                          fetchrecursUserData.add(approvaldto_Key);
                      }

                  }

              }
        catch (JSONException e) {
            e.printStackTrace(); 
            }

          return  fetchrecursUserData;

    }
Hiteshpatel0024
  • 593
  • 1
  • 4
  • 14
0

You can try below code

String serial= jsonObject.getJSONObject("response").getString("serialNumber");

or

JSONObject json;
try {
    json = new JSONObject(buffer.toString());
    String accessToken = json.getString("access_token");
    return accessToken;
} catch (JSONException e) {
    Log.e("Podcast", "There was an error", e);
}
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Nirali
  • 13,571
  • 6
  • 40
  • 53