0

I'm trying to use JSONObject's put method in for loop. But I'm not getting Expected output.

Expected output:

{"Result":[{"PostOfficeName":"Bhajan Pura","Pincode":"110096"},{"PostOfficeName":"Gokulpuri","Pincode":"110094"}, and so on...]}

OutPut I'm getting:

{"Result":[{"PostOfficeName":"Bhajan Pura","Pincode":"110096"}]}

here is my code:

try {
            JSONObject obj = new JSONObject(loadJsonfromAssets());
            JSONArray arr = obj.getJSONArray("Sheet1");

            JSONObject finalObj = new JSONObject();
            JSONArray ResultArray = new JSONArray();
            JSONObject infoObj = new JSONObject();

            for (int i=0; i<arr.length();i++){
                JSONObject obj1 = arr.getJSONObject(i);
                if (obj1.getString("City").equals("New Delhi")){
Log.d("postal", "Found!");
Toast.makeText(this, "" + obj1.getString("Pincode"), Toast.LENGTH_SHORT).show();
infoObj.put("PostOfficeName", obj1.getString("PostOfficeName"));
                    infoObj.put("Pincode",obj1.getString("Pincode"));
                    ResultArray.put(infoObj);
                }
            }
            finalObj.put("Result", ResultArray);

            System.out.println(finalObj);
   
        } catch (JSONException e) {
            e.printStackTrace();
        }
1
  • 1
    Try to create the jsonobject inside loop in which you are setting the pincode etc then keep adding into list by which when loop moves to next index you have empty jsonobject. And at last outside loop add the list into final json object against result key Commented Jun 23, 2021 at 16:33

1 Answer 1

1

Have you tried moving the construction of infoObj inside the loop. By having it outside, you're maintaining state across loop iterations. I suspect you're just updating the same json object each time and adding it to the JSON array. Not sure why you NOT getting duplicates because I do when I run your code.

Change it to this makes it "better"

try {
    JSONObject obj = new JSONObject(loadJsonfromAssets());
    JSONArray arr = obj.getJSONArray("Sheet1");

    JSONObject finalObj = new JSONObject();
    JSONArray ResultArray = new JSONArray();

    for (int i=0; i<arr.length();i++){
        JSONObject obj1 = arr.getJSONObject(i);
        if (obj1.getString("City").equals("New Delhi")) {
          Log.d("postal", "Found!");
          Toast.makeText(this, "" + obj1.getString("Pincode"), 
          Toast.LENGTH_SHORT).show();

          // move instantiation INSIDE loop
          JSONObject infoObj = new JSONObject();

          infoObj.put("PostOfficeName", obj1.getString("PostOfficeName"));
          infoObj.put("Pincode",obj1.getString("Pincode"));
          ResultArray.put(infoObj);
        }
    }
    finalObj.put("Result", ResultArray);

    System.out.println(finalObj);

} catch (JSONException e) {
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

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.