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.
Community
  • 1
  • 1
Fallenreaper
  • 8,518
  • 10
  • 53
  • 107

2 Answers2

3

Use this:

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

See the docs for JSONTokener.

Femi
  • 62,839
  • 8
  • 114
  • 142
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....

Arpit Patel
  • 1,596
  • 12
  • 23
  • .getsString(index) doesnt seem to be a call to JSONObject – Fallenreaper Oct 04 '12 at 16:57
  • You have to have the for loop till you JSONARRAY. your index is for the data from all the array values. – Arpit Patel Oct 04 '12 at 17:04
  • 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' – Fallenreaper Oct 04 '12 at 17:06
  • just look at my answer i have just given it. http://stackoverflow.com/questions/12731970/android-httppost-is-downloading-wrong-ssl-cerficate/12732158#12732158 This will surely helps you how to iterate with the JSON. – Arpit Patel Oct 04 '12 at 17:10