0

It's been a while and i'm trying to ignore some frustrating issue i'm having with json things in java, i'm new to this and read alot however, parsing json in javascript or php was alot better (or easier i dunno) but now in java i cannot convert a jsonobject to jsonarray if it doesn't have a parent, cuz it uses .getJsonArray('array)

BUT what IF i have this :

{"49588":"1.4 TB","49589":"1.4 TB MultiAir","49590":"1.4 TB MultiAir TCT","49591":"1.6L MultiJet","49592":"1750 Tbi","49593":"2.0L MultiJet","49594":"2.0L MultiJet TCT"}

i'm not succeeding in anyway to convert it to array

what i want is to convert this JSONObject to JSONArray loop within its items and add them to a Spinner, now that's the first issue, the second question is: if i convert this to JSONArray how can i add the ID, Text to the spinner? just like the HTML Select tag

<option value="0">Item 1</option>

so it's an issue and a question hope someone can find the solution for this jsonarray thing, without modifying the json output from the website, knowing that if i modify and add a parent to this json, the JSONArray will work. but i want to find the solution for that.


Nothing special i have in the code:

Just a AsynTask Response, a log which is showing the json output i put at the beginning of this question

Log.d("response", "res " + response);

// This will work
jsonCarsTrim = new JSONObject(response);

// This won't work
JSONArray jdata = new JSONArray(response);

Thanks !

Emad Ha
  • 1,203
  • 15
  • 33
  • Can you show your code ? – 2Dee Dec 06 '13 at 13:26
  • duplicate http://stackoverflow.com/questions/7634518/getting-jsonobject-from-jsonarray – Nitin Misra Dec 06 '13 at 13:27
  • Not really @NitinMisra, he's getting an array and looping through it using for, but i'm not even getting an array, and cannot loop through jsonobject without Iterator, i don't wanna use that, i want to know how to convert this type to an array cuz i've been facing this issue alot now, and need a solution, don't want to modify the website output – Emad Ha Dec 06 '13 at 13:35

3 Answers3

3

How about this:

JSONObject json = new JSONObject(yourObject);
Iterator itr = json.keys();

ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
ArrayList<Integer> links = new ArrayList<Integer>();

int i = 0;
while(itr.hasNext()) {
    String key = itr.next().toString();
    links.add(i,Integer.parseInt(key));
    entries.add(i, (CharSequence) json.getString(key)); //for example
    i++;
}

//this is the activity
 entriesAdapter = new ArrayAdapter<CharSequence>(this,
            R.layout.support_simple_spinner_dropdown_item, entries);
//spinner is the spinner the data is added too
spinner.setAdapter(entriesAdapter);

this should work (works for me), you may have to modify the code. The way shown, i am adding all entries of the json object into a Spinner, where my json key is the index value and the linked String value of the json object will be shown as Spinner entry (title) in my activity.

Now when an Item is selected, fetch the SelectedItemPosition and you can look it up in the "links" array list, to get the real value.

bbuecherl
  • 1,609
  • 12
  • 20
  • i used that for previous objects it works, but i want to find the solution for my specific issue, otherwise someone should tell me it doesn't work, then i won't be looking for a solution, and i think there's a solution for that at least manualy exploding the comma character. my other question is how to add this value/text to the spinner – Emad Ha Dec 06 '13 at 13:37
  • What's the "values" ? – Emad Ha Dec 06 '13 at 13:48
  • sorry, no values there (copy and paste mistake), I used a very similar code in one of my recent projects – bbuecherl Dec 06 '13 at 13:51
  • looks promising, but i'm getting two errors : The method add(int, CharSequence) in the type ArrayList is not applicable for the arguments (String, String) – Emad Ha Dec 06 '13 at 13:55
  • worked so far, but getting error on runtime : **ArrayList.throwIndexOutOfBoundsException(int, int) line: 255** (index: 123, size : 0) – Emad Ha Dec 06 '13 at 14:06
  • Does it have to be CharSquence ? – Emad Ha Dec 06 '13 at 14:08
  • i accepted your answer anyway, but if you can assist me with the spinner issue ill be thankful – Emad Ha Dec 06 '13 at 14:11
  • Most android library objects use CharSequences instead of String, but the conversion is quite easy, in fact [String implements CharSequence](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html) so you can use Strings instead. – bbuecherl Dec 06 '13 at 14:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42653/discussion-between-emad-and-mindsonic) – Emad Ha Dec 06 '13 at 15:30
2

I'm not sure if this is thing you want but give it a try. There is tutorial to convert the Json to Map. After you convert it, you can iterate through the map.

http://www.mkyong.com/java/how-to-convert-java-map-to-from-json-jackson/

osayilgan
  • 5,873
  • 7
  • 47
  • 68
0

What you have is a JSON object type, not an array type. To iterate you can get the Iterator from the keys method.

Rajesh
  • 15,724
  • 7
  • 46
  • 95
  • 1 - not possible to convert it to a jsonarray ? 2 - how can i add this value,text to a spinner through Iterator? – Emad Ha Dec 06 '13 at 13:32