13

How can I parse in Android a Json array of strings and save it in a java string array ( like: xy[ ] ) ?

My Json to be parsed :

 [
  {
    "streets": [ "street1", "street2", "street3",... ],
  }
 ]

Later in my code I want to populated with that array a spinner item in my layout. Everything i tried enden with only one street item listed in the spinner.

3
  • You mean array of array of Strings?? Commented Oct 5, 2013 at 11:04
  • Please post your full code. Commented Oct 5, 2013 at 11:21
  • Actually its an array of objects of arrays of strings Commented Oct 5, 2013 at 12:36

3 Answers 3

40

To parse

try {

   JSONArray jr = new JSONArray("Your json string");
   JSONObject jb = (JSONObject)jr.getJSONObject(0);
   JSONArray st = jb.getJSONArray("streets");
   for(int i=0;i<st.length();i++)
       {
      String street = st.getString(i);
      Log.i("..........",""+street);
      // loop and add it to array or arraylist
        }
}catch(Exception e)
{
        e.printStackTrace();
}

Once you parse and add it to array. Use the same to populate your spinner.

[ represents json array node

{ represents json object node

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

11 Comments

@Hesam then you are doing it wrong not because the parsing code posted is wrong
Thanks for fast reply. I have and array exactly like question and Array contains 5 urls. I did your way, the Intelij Idea is saying "Variable 'url' initializer '(String) jFaceShape.get(j)' is redundant". Also, when I compile I can see my loop but no string is inside. This is my code: JSONArray jFaceShape = jObject.optJSONArray(Product.TAG_FACE_SHAPES); for(int j=0; j<jFaceShape.length(); j++) { String url = (String) jFaceShape.get(j); Log.d(TAG, "url => " + url);}
@Hesam post your json. you are doing it wrong. It should work irrespective of the IDE that you use
I appreciate a lot for your time man. Please look at this url hijab2go.com/?route=feed/web_api/products
|
3

Try this..

 JSONArray arr = new JSONArray(json string);

    for(int i = 0; i < arr.length(); i++){

            JSONObject c = arr.getJSONObject(i);        
            JSONArray ar_in = c.getJSONArray("streets");

        for(int j = 0; j < ar_in.length(); j++){    
            Log.v("result--", ar_in.getString(j));
        }
   }

Comments

2

We need to make JSON object first. For example,

JSONObject jsonObject = new JSONObject(resp);
// resp is your JSON string
JSONArray arr  = jsonObject.getJSONArray("results");
Log.i(LOG, "arr length =  " + arr.length());
for(int i=0;i<arr.length();i++)
{...

arr may contains other JSON Objects or JSON array. How to convert the JSON depends on the String. There is a complete example with some explanation about JSON String to JSON array can be found at http://www.hemelix.com/JSONHandling

Comments

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.