Question

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 !

Was it helpful?

Solution

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.

OTHER TIPS

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/

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top