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 !

3
  • Can you show your code ? Commented Dec 6, 2013 at 13:26
  • duplicate stackoverflow.com/questions/7634518/… Commented Dec 6, 2013 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 Commented Dec 6, 2013 at 13:35

3 Answers 3

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.

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

9 Comments

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
What's the "values" ?
sorry, no values there (copy and paste mistake), I used a very similar code in one of my recent projects
looks promising, but i'm getting two errors : The method add(int, CharSequence) in the type ArrayList<CharSequence> is not applicable for the arguments (String, String)
worked so far, but getting error on runtime : ArrayList.throwIndexOutOfBoundsException(int, int) line: 255 (index: 123, size : 0)
|
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/

1 Comment

nice link ... really helpful
0

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

1 Comment

1 - not possible to convert it to a jsonarray ? 2 - how can i add this value,text to a spinner through Iterator?

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.