user3547706
user3547706

Reputation:

Issue ion parse JSON data in android

i want to parse data like below format in this format there are no any left side value for parse it so any idea how can i make it possible

JSON FORMAT

{
"labels": [
    "Dec-2014",
    "Jan-2015",
    "Feb-2015",
    "Mar-2015",
    "Apr-2015",
    "May-2015"
],
"data": [
    0,
    0,
    0,
    0,
    0,
    0
  ]
}

Upvotes: 1

Views: 252

Answers (3)

Krishna V
Krishna V

Reputation: 1821

You can get jsonData as follows.

JSONObject jsonObj=new JSONObject(urJson);
    JSONArray labels=jsonObj.getJSONArray("labels");
    ArrayList<String> lableList=new ArrayList<String>();

    for (int i = 0; i < labels.length();i++) 
    {
       lableList.add(labels.getString(i));
    }

    JSONArray data=jsonObj.getJSONArray("data");
    ArrayList<String> dataList=new ArrayList<String>();

    for (int i = 0; i < data.length();i++) 
    {
       dataList.add(data.getString(i));
    }

Upvotes: 0

Sandeep Singh
Sandeep Singh

Reputation: 1127

Try this

  try {

        JSONObject jsonObject = new JSONObject(
                "{\"labels\": [\"Dec-2014\",\"Jan-2015\",\"Feb-2015\",\"Mar-2015\",\"Apr-2015\",\"May-2015\"],\"data\": [0,0,0,0,0,0]}");

        JSONArray array = jsonObject.getJSONArray("labels");

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

            String s = (String) array.get(i);
            System.out.println(s);
        }

        JSONArray array2 = jsonObject.getJSONArray("data");

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

            String s = (String) array2.get(i);
            System.out.println(s);
        }

    } catch (Exception e) {

    }

Hope this will solve your problem!!!

Upvotes: 1

Vinay Jayaram
Vinay Jayaram

Reputation: 1005

There are two array objects in your current Json, You can extract it as given below

   // Data  json array
   JSONArray dataArry = obj.getJSONArray("genre");
   ArrayList<String> data = new ArrayList<String>();
    for (int j = 0; j < dataArry.length(); j++) 
     {
       data.add((String) dataArry.get(j));
     }

Same logic can be applied for labels too

Upvotes: 0

Related Questions