1

I'm trying to take the following string that I got as a json object:

 [
    {
        "id": "picture1",
        "caption": "sample caption",
        "picname": "sample picture name"
      }
 ]

and turn it into a array so I can populate a list

I've tried turning it into a jsonarray by doing this:

JSONArray myjsonarray = myjson.toJSONArray(string_containing_json_above); 

but that didn't seem to work.

==============

Here is full code with the working solution

myjson = new JSONObject(temp);
String String_that_should_be_array = myjson.getString("piclist");
JSONArray myjsonarray = new JSONArray(String_that_should_be_array);
For(int i = 0; i < myjsonarray.length(); i++){
    JSONObject tempJSONobj = myjsonarray.getJSONObject(i);
    showToast(tempJSONobj.get("caption").toString());
}

temp is the json from the server

3
  • What is myson? And what is temparray? Have you looked at the JavaDoc for JSONArray? Take note of the constructor that accepts a string. Commented Apr 11, 2013 at 6:12
  • I edited the post to make more sense. I'm sorry. temparray is a string but i've renamed the variable in my code because it was a terrible variable name Commented Apr 11, 2013 at 6:23
  • Ok, so that clears some stuff up. But you didn't mention what 'myjson' is. But, see answer from @PareshMayani. Commented Apr 11, 2013 at 6:26

3 Answers 3

4

Issue is here:

JSONArray myjsonarray = myjson.toJSONArray(temparray); 

Solution:

JSONArray myjsonarray = new JSONArray(myJSON);   
// myJSON is String

Now here you are having JSONArray, iterate over it and prepare ArrayList of whatever types of you want.

2
  • Sorry I can only mark one as correct but this is correct too. Thank you Paresh Mayani! Commented Apr 11, 2013 at 6:28
  • I would upvote you but I don't have the reputation to upvote yet. When I do I will. Thank you again. Commented Apr 11, 2013 at 6:38
3

here you get JSONArray so change

JSONArray myjsonarray = myjson.toJSONArray(temparray); 

line as shown below

JSONArray jsonArray = new JSONArray(readlocationFeed);

and after

 JSONArray jsonArray =  new JSONArray(readlocationFeed);

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject explrObject = jsonArray.getJSONObject(i);
        explrObject.getString("caption");
}
0
0

JSONArray isn't working because the JSON you provided is not an array. You can read more about JSON syntax here: http://www.w3schools.com/json/json_syntax.asp

In the meantime, you could manually create your array by paring the JSON one string at a time.

JSONObject strings = new JSONObject(jsonString);
String array[] = new String[5];

if (jsonString.has("id"){
    array[0] = jsonString.getString("id");
}
if (jsonString.has("caption"){
    array[1] = jsonString.getString("caption");
}
...

etc.

2
  • Do you really prefer String[] over ArrayList<String>? Commented Apr 11, 2013 at 6:19
  • No, I guess I just thought that since the question specified an "array" that that was what s/he wanted. Commented Apr 11, 2013 at 16:49

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.