0

I saw this link: How to parse a JSON and turn its values into an Array? and thought it was useful. This would work for me, except 1 thing. I dont have a referencer. I have:

 ["some_long_string"]

and that is it as the data. I dont know why it is like this, but when i check to log, thats what the data in my Java spits out.

With the example in the above link, i was looking at the JSONObject with Java, denoted also by their site: http://www.json.org/java/

I see an option to resolve this but not quite sure on syntax. If you can help that would be great. My attempt is something like:

 JSONObject myjson = new JSONObject(data);
 //String item = myjson.getStrings()[0]; //or something like this.
2
  • [] : it is an array, not an object, read the documentation again Commented Oct 4, 2012 at 16:40
  • true true I know that. i misspoke Commented Oct 4, 2012 at 17:01

2 Answers 2

3

Use this:

JSONArray arr = (JSONArray)new JSONTokener(sourceString).nextValue();

See the docs for JSONTokener.

Sign up to request clarification or add additional context in comments.

3 Comments

and then since it is an array, just reference it accordingly? arr[0].toString() ?
or would it moreso look like: path = (String)new JSONTokener(path).nextValue();
you are correct. Thanks for the info. What you have to do is: //..what you did above. String item = arr.getString(0);
1

Take a ArrayList and parse the whole strings to the Arraylist just see following:

ArratList<string> list = new ArrayList<string>();

list.add(yourJSONObject.getString(index));
list.add(yourJSONObject.getString(index));

you can add one by one all the values to the ArrayList and parse strings to ArrayList.You can also use it as the multiple values like the following like if you want to parse the multiple datatype JSON then you can use the following step. Firstly create one class with all the datatypes.

class multipledatatypes{
    int id;
    String a,b,c;
    Double d;
}

Now parse this class to Arralist and insert the value like following

ArrayList<multipledatatypes> list = new ArrayList<multipledatatypes>();
 for (int i = 0; i < JArray_cat.length(); i++) {
         multipledatatypes mdto = new multipledatatypes();
         mdto.id = yourJSONObject.getInt(index);
         mdto.a = yourJSONObject.getString(index);
         mdto.b = yourJSONObject.getString(index);
         mdto.c = yourJSONObject.getString(index);
         mdto.d = yourJSONObject.getDouble(index);
         list.add(mdto);
    }

This is how to can parse the data to the Arraylist and use it in your code.

Hope this will help you....

4 Comments

.getsString(index) doesnt seem to be a call to JSONObject
You have to have the for loop till you JSONARRAY. your index is for the data from all the array values.
yea, i was unaware of how to do the JSON iteration. I will give you a +1 if you adjust code to show it and adjust your first code block to not say getsString() as there is an extra 's'
just look at my answer i have just given it. stackoverflow.com/questions/12731970/… This will surely helps you how to iterate with the JSON.

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.