0
  [  {
        "question_id": 13,
        "creation_date": 10,
        "name": " certain degrees",
        "tags": [
            "android",
            "sensor"
        ],
.....
...}

]

Here is the api I want to get the tags array ,the tag array contains only index and value,i want to get the array using array object and finally store in array list, i have tried but fail to get the error has been occured.

02-20 15:45:37.323: I/System.out(1165): org.json.JSONException: Value android at 0 of type java.lang.String cannot be converted to JSONObject

could anybody help me to solve this problem.

tagarray = questionobj.getJSONArray("tags");


        for(int j=0;j<tagarray.length();j++){



//tagobject = tagarray.getJSONObject(j);//..if i comment this line the full array list is displaying but i need for each question allocated tags to display..am i clear.. 
            //if(tagobject!=null){
            tagname.add(tagarray.getString(j));
            ///tagname.add(0, null);
            //} 
        }
6
  • just comment tagobject = tagarray.getJSONObject(j); line because u are getting only values inside JSONArray Commented Feb 20, 2013 at 10:21
  • why the same question again?
    – Rahul
    Commented Feb 20, 2013 at 10:21
  • @�?я�?ѕ�?єяK i will do so..the answer wat i want i am not getting that
    – user2047120
    Commented Feb 20, 2013 at 10:22
  • @R.J hope anybody can help me out thats why
    – user2047120
    Commented Feb 20, 2013 at 10:23
  • people will reply to your post there. Posting same questions can only create duplicates here, and you may yourself lose track of it.
    – Rahul
    Commented Feb 20, 2013 at 10:26

4 Answers 4

0

your json format is not correct try like that;

{"question":
{"question_id": "13", 
 "name": "certain degrees", 
 "tags": [{"android":"N", "sensor":"Y"}]
 }}

You can use http://jsonviewer.stack.hu/, to be sure correction of your json data

0

I sugest you try this code,

import org.json.JSONArray;
import org.json.JSONObject;

public class Main {

    public static void main(String[] args) {
        String text = "[  { \"question_id\": 13, \"creation_date\": 10, \"name\": \"certain degrees\", \"tags\": [ \"android\", \"sensor\" ] } ]";

        System.out.println(text);

        JSONArray jsonArray = new JSONArray(text);

        JSONObject jsonObjetc = jsonArray.getJSONObject(0);

        JSONArray tagarray = jsonObjetc.getJSONArray("tags");

        System.out.println(tagarray);

        for(int j=0;j<tagarray.length();j++){
            System.out.println(tagarray.getString(j));
        }
    }
}

Good luck, Aron

0

Try this,

   ArrayList<String> list = new ArrayList<String>();

String text = "[  { \"question_id\": 13, \"creation_date\": 10, \"name\": \"certain degrees\", \"tags\": [ \"android\", \"sensor\" ] } ]";
  try {
        JSONArray jarray = new JSONArray(text);

        for (int i = 0; i < jarray.length(); i++) {
            JSONArray j_array = jarray.getJSONObject(i)
                    .getJSONArray("tags");

            for (int j = 0; j < j_array.length(); j++) {
                String str = j_array.getString(j);

                list.add(str);

            }

        }
    } catch (Exception e) {

    }
0

Try This

tagarray = questionobj.getJSONArray("tags");

    for(int j=0;j<tagarray.length();j++)
    {
        tagname.add(tagarray.getJSONObject(j).getString("android"));         
    }