0

I've a JSON like this

{

      "result":
 {

    "issue_date": "xx-yy-zzzz",
    "father/husband": "TEST",
    "name": "ABC ",
    "blood_group": "",
    "dob": "xx-yy-zzzz",
    "validity": {
      "non-transport": "xx-yy-zzzz to xx-yy-zzzz",
      "transport": "xx-yy-zzzz to xx-yy-zzzz"
    },
    "cov_details": {
      "MCWG": "NA",
      "3WTR": "NA",
      "PSV BUS": "NA",
      "LMV": "NA",
      "INVCRG": "NA"
    },
    "address": "ABC"
  }
}

     JSONObject dlData = new JSONObject();
     JSONObject dlObj = new JSONObject();
     JSONObject dlcov = new JSONObject();
     JSONObject dlCovs = new JSONObject();
    dlCov = jsonObject.getJSONObject("result").getJSONObject("cov_details");

to access the data of cov_details, i'm using this code block to store the details present within cov_details Object

         dlcov = jsonObject.getJSONObject("result").getJSONObject("cov_details");
        Iterator<String> x = dlcov.keys();
            while (x.hasNext()){
                String key1 = x.next();
                String value1 = dlcov.optString(key1);
                dlCovs.put("covabbrv",key1);
                dlCovs.put("dcIssuedt",value1);
                dlCovs.put("vecatg",key1);

            }

        dlData.put("dlCovs", dlCovs);

i am trying to store every values in dlCovs but it stores only last values in the object, is their anyway using which i can store all the value in dlCovs objet with it's Key value and iterate over it.Any help will be highly appreciable, thanks well in advance. enter code here

6
  • Where is dlCovs defined? Commented Jan 4, 2017 at 1:57
  • shmosel it's another JSONObject defined within the scope only, i forgot to mention that, JSONObject dlCovs = new JSONObject(); Commented Jan 4, 2017 at 2:03
  • Please edit your post with any additional code. Commented Jan 4, 2017 at 2:04
  • Modified code (y) Commented Jan 4, 2017 at 2:09
  • Do you want a JSONObject or a JSONArray as the value of "dlCovs" in dlData? Looks like you would need an array. Commented Jan 4, 2017 at 2:22

2 Answers 2

1

Use JSONArray for covabbrv, dcIssuedt and vecatg

JSONArray covabbrv = new JSONArray();
JSONArray dcIssuedt = new JSONArray();
JSONArray vecatg = new JSONArray();
Iterator<String> x = dlcov.keys();
    while (x.hasNext()){
        String key1 = x.next();
        String value1 = dlcov.optString(key1);
        covabbrv.put(key1);
        dcIssuedt.put(value1);
        vecatg.put(key1);
    }
dlCovs.put("covabbrv",covabbrv);
dlCovs.put("dcIssuedt",dcIssuedt);
dlCovs.put("vecatg",vecatg);

Output would be somthing like:

{
    "covabbrv" : ["MCWG", "3WTR", "PSV BUS", "LMV", "INVCRG"],
    "dcIssuedt" : ["NA", "NA", "NA", "NA", "NA"],
    "vecatg" : ["MCWG", "3WTR", "PSV BUS", "LMV", "INVCRG"]
}
Sign up to request clarification or add additional context in comments.

Comments

0

With dlCovs as a JSONArray, I would do something like this:

JSONArray dlCovs = new JSONArray();
dlcov = jsonObject.getJSONObject("result").getJSONObject("cov_details");
Iterator<String> x = dlcov.keys();
while (x.hasNext()) {
    String key1 = x.next();
    String value1 = dlcov.optString(key1);

    JSONObject currentDlCov = new JSONObject();
    currentDlCov.put("covabbrv",key1);
    currentDlCov.put("dcIssuedt",value1);
    currentDlCov.put("vecatg",key1);

    dlCovs.add(currentDlCov);
}

dlData.put("dlCovs", dlCovs);

This will add an array of JSONObjects as a value of dlCovs if your dlData JSONObject.

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.