1

I am trying to parse a JSON String and map it to a hashmap, I have a valid JSONString from server but when I traverse through it, all I can get it is the first result.

 JSONArray peoples = null;
 ArrayList<HashMap<String, String>> personList = new ArrayList<HashMap<String,String>>();

JSONObject jsonObj = new JSONObject(value);
                Log.d("Jello",jsonObj.toString());
                peoples = jsonObj.getJSONArray("product");//check here
                Log.d("Jello",peoples.toString());


                for(int i=0;i<peoples.length();i++){
                    JSONObject c = peoples.getJSONObject(i);
                    String service_group = c.getString("sgroup");
                    String service = c.getString("service");
                    String value = c.getString("value");
                    String updated_at = c.getString("updated_at");

                    HashMap<String,String> persons = new HashMap<String,String>();

                    persons.put("service_group",service_group);
                    persons.put("service",service);
                    persons.put("value",value);
                    persons.put("updated_at",updated_at);


                    personList.add(persons);
                }

My JSON String is :

{"product":[{"sgroup":"Dummy_BIG_ONE","service":"Dummy_UNDER_BIG_ONE","code":"128","value":"0","updated_at":"2015-12-04 21:21:00"}]}{"product":[{"sgroup":"Hello Monkey","service":"Do u work","code":"123","value":"0","updated_at":"2015-12-04 21:27:51"}]}{"product":[{"sgroup":"Checking from Android Device","service":"Monkey","code":"12345","value":"0","updated_at":"2015-12-04 22:55:39"}]}{"product":[{"sgroup":"Checking from Android Device","service":"Monkey","code":"12345","value":"0","updated_at":"2015-12-04 22:55:40"}]}{"product":[{"sgroup":"Checking from Android Device","service":"Monkey","code":"12345","value":"0","updated_at":"2015-12-04 22:55:42"}]}{"product":[{"sgroup":"Hello World","service":"Donkey","code":"24411","value":"0","updated_at":"2015-12-04 22:57:05"}]}{"product":[{"sgroup":"lkfnhjdiofho","service":"dfjdifj","code":"1101","value":"0","updated_at":"2015-12-05 01:15:49"}]}{"product":[{"sgroup":"Baal","service":"Saal","code":"1234","value":"21","updated_at":"2015-12-05 01:34:59"}]}{"product":[{"sgroup":"Inis","service":"Mona","code":"1234","value":"1001","updated_at":"2015-12-05 01:39:51"}]}{"product":[{"sgroup":"Medical Treatment Loan","service":"Number of referral slip","code":"128","value":"0","updated_at":"2015-12-05 01:50:42"}]}{"product":[{"sgroup":"Medical Treatment Loan","service":"Number of referral slip","code":"128","value":"0","updated_at":"2015-12-05 01:55:12"}]}{"product":[{"sgroup":"Medical Treatment Loan","service":"Number of referral slip","code":"128","value":"1000","updated_at":"2015-12-05 01:56:10"}]}

or HERE

here is how I am sending the JSON

while($row = mysql_fetch_assoc($output))
        {
            $product = array();
            $product["sgroup"] = $row["service_group"];
            $product["service"] = $row["service"];
            $product["code"] = $row["code"];
            $product["value"] = $row["amount"];
            $product["updated_at"] = $row["updated_at"];

            // user node
            $response["product"] = array();

            array_push($response["product"], $product);

            // echoing JSON response
            echo json_encode($response);
        }

I want to add all the data's from the JSON in my ArrayList so that I can use it later. Thank you for your help.

4
  • 3
    Your JSON is invalid. Commented Dec 5, 2015 at 0:23
  • Then How can I send all the JSON individually one after another? I am adding them all in an array. Commented Dec 5, 2015 at 0:24
  • Instead of calling echo json_encode() from inside the loop, add to one large array, and then echo json_encode() the full array after the loop. Also note that you'll want the outer element to be a JSONArray instead of a JSONObject, so you will need to change your parsing code accordingly. Commented Dec 5, 2015 at 0:45
  • @DanielNugent, can you please give me a hint? I am stuck in here for a long time. :/ Commented Dec 5, 2015 at 0:50

2 Answers 2

1

First, there are online tools available to determine if your JSON is valid.

Here is a general example of a better way to format your JSON data:

<?php 
    $bigArray = array();

    for ($x = 0; $x <= 10; $x++) {
            $product = array();
            $product["sgroup"] = $x;
            $product["service"] = $x;
            $product["code"] = $x;
            $product["value"] = $x;
            $product["updated_at"] = $x;

            array_push($bigArray, $product);
        }
      // echoing JSON response
      echo json_encode($bigArray);      
?>

Which gives this valid JSON response that is simple and easy to parse:

[
   {
      "sgroup":0,
      "service":0,
      "code":0,
      "value":0,
      "updated_at":0
   },
   {
      "sgroup":1,
      "service":1,
      "code":1,
      "value":1,
      "updated_at":1
   },
   {
      "sgroup":2,
      "service":2,
      "code":2,
      "value":2,
      "updated_at":2
   },
   {
      "sgroup":3,
      "service":3,
      "code":3,
      "value":3,
      "updated_at":3
   },
   {
      "sgroup":4,
      "service":4,
      "code":4,
      "value":4,
      "updated_at":4
   },
   {
      "sgroup":5,
      "service":5,
      "code":5,
      "value":5,
      "updated_at":5
   },
   {
      "sgroup":6,
      "service":6,
      "code":6,
      "value":6,
      "updated_at":6
   },
   {
      "sgroup":7,
      "service":7,
      "code":7,
      "value":7,
      "updated_at":7
   },
   {
      "sgroup":8,
      "service":8,
      "code":8,
      "value":8,
      "updated_at":8
   },
   {
      "sgroup":9,
      "service":9,
      "code":9,
      "value":9,
      "updated_at":9
   },
   {
      "sgroup":10,
      "service":10,
      "code":10,
      "value":10,
      "updated_at":10
   }
]

As for the parsing, using a HashMap is not the best way. Create a list of Person POJO objects.

First define the Person class:

class Person {
    public String group;
    public String service;
    public String value;
    public String updated;
    public Person(String g, String s, String v, String u) {
        group = g;
        service = s;
        value = v;
        updated = u;
    }
}

Then, parsing is fairly simple:

    List<Person> personList = new ArrayList<>();
    JSONArray jsonArr;
    try {
        jsonArr = new JSONArray(response);
        for(int i=0;i<jsonArr.length();i++){
            JSONObject c = jsonArr.getJSONObject(i);
            String service_group = c.getString("sgroup");
            String service = c.getString("service");
            String value = c.getString("value");
            String updated_at = c.getString("updated_at");

            Person p = new Person(service_group, service, value, updated_at);

            personList.add(p);
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Daniel. You are a life saver :) We managed to submit our first project in due time in a hackathon :)
0

You should put your products into json array:

[
  {
    "product": {
      "sgroup": "lkfnhjdiofho",
      "service": "dfjdifj",
      "code": "1101",
      "value": "0",
      "updated_at": "2015-12-05 01:15:49"
    }
  },
  {
    "product": {
      "sgroup": "Baal",
      "service": "Saal",
      "code": "1234",
      "value": "21",
      "updated_at": "2015-12-05 01:34:59"
    }
  },
  {
    "product": {
      "sgroup": "Inis",
      "service": "Mona",
      "code": "1234",
      "value": "1001",
      "updated_at": "2015-12-05 01:39:51"
    }
  },
  {
    "product": {
      "sgroup": "Medical Treatment Loan",
      "service": "Number of referral slip",
      "code": "128",
      "value": "0",
      "updated_at": "2015-12-05 01:50:42"
    }
  },
  {
    "product": {
      "sgroup": "Medical Treatment Loan",
      "service": "Number of referral slip",
      "code": "128",
      "value": "0",
      "updated_at": "2015-12-05 01:55:12"
    }
  },
  {
    "product": {
      "sgroup": "Medical Treatment Loan",
      "service": "Number of referral slip",
      "code": "128",
      "value": "1000",
      "updated_at": "2015-12-05 01:56:10"
    }
  }
]

And you have to change your parser little bit:

JSONArray jsonArray = new JSONArray(string);

    for(int i=0;i<jsonArray.length();i++){
        JSONObject c = jsonArray.getJSONObject(i).getJSONObject("product");
        String service_group = c.getString("sgroup");
        String service = c.getString("service");
        String value = c.getString("value");
        String updated_at = c.getString("updated_at");

        HashMap<String,String> persons = new HashMap<String,String>();

        persons.put("service_group",service_group);
        persons.put("service",service);
        persons.put("value",value);
        persons.put("updated_at",updated_at);


        personList.add(persons);
    }

You don't need put your object into "product" attribute, but directly into array :

[
  {
    "sgroup": "lkfnhjdiofho",
    "service": "dfjdifj",
    "code": "1101",
    "value": "0",
    "updated_at": "2015-12-05 01:15:49"
  },
  {
    "sgroup": "Baal",
    "service": "Saal",
    "code": "1234",
    "value": "21",
    "updated_at": "2015-12-05 01:34:59"
  },

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.