Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
What is myson? And what is temparray? Have you looked at the JavaDoc for JSONArray? Take note of the constructor that accepts a string. – Perception Apr 11 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 – TheMcMurder Apr 11 at 6:23
Ok, so that clears some stuff up. But you didn't mention what 'myjson' is. But, see answer from @PareshMayani. – Perception Apr 11 at 6:26

3 Answers

up vote 1 down vote accepted

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");
}
share|improve this answer
Perfect!!! Thank you that worked wonderfully. – TheMcMurder Apr 11 at 6:27
WC :)......,,,, – kyogs Apr 11 at 6:31

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.

share|improve this answer
Do you really prefer String[] over ArrayList<String>? – Paresh Mayani Apr 11 at 6:19
No, I guess I just thought that since the question specified an "array" that that was what s/he wanted. – dckuehn Apr 11 at 16:49

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.

share|improve this answer
Sorry I can only mark one as correct but this is correct too. Thank you Paresh Mayani! – TheMcMurder Apr 11 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. – TheMcMurder Apr 11 at 6:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.