Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have some JSON with the following structure:

{"cit": [
    "ALL",
    "Aceh Barat",
    "Aceh Besar",
    "Aceh Jaya",
    "Aceh Selatan",
    "Aceh Tengah",
    "Aceh Timur",
    "Aceh Utara"]}

i have try to parsing my json like this :

JSONObject jsonObject = new JSONObject(result);
JSONArray city=jsonObject.getJSONArray("cit");

for (int j=0; j < city.length(); j++){
    JSONObject cit = city.getJSONObject(j);
    String kot = cit.toString(j);

    kota.add(kot);
}

on post execute :

ArrayAdapter<String> SpinnerKota = new ArrayAdapter<String>(Pencarian.this, android.R.layout.simple_list_item_1, kota);
spin_kota.setAdapter(SpinnerKota);  

but nothing happen, is there any wrong with my code? i hope someone can help me to solve my problem.

share|improve this question
    
How do you know nothing's happening? Are you getting some kind of stack? Are you not getting expected output? (The snippet you posted doesn't indicate if you're going to get output one way or another.) –  Makoto Oct 14 '13 at 4:36
1  
use this String kot=city.get(j) –  Raghunandan Oct 14 '13 at 4:37
    
in my spinner it will show as : [ "ALL", "Aceh Barat", "Aceh Besar", "Aceh Jaya", "Aceh Selatan", "Aceh Tengah", "Aceh Timur", "Aceh Utara"] and i think this is not true –  Aoyama Nanami Oct 14 '13 at 4:42
    
@AoyamaNanami the spinner will show the above, because that is what is there in the json. What's wrong with it ? –  Rat-a-tat-a-tat Ratatouille Oct 14 '13 at 5:19
add comment

4 Answers

up vote 1 down vote accepted
"cit": [ // json array cit
"ALL",  // index 0 is ALL

Also there is no json object inside json array cit. So you don't need this JSONObject cit = city.getJSONObject(j).

Change

  String kot = cit.toString(j);

To

  String kot =  (String) city.get(j);

Use the below

      JSONObject jsonObject = new JSONObject("myjson string");
      JSONArray city=jsonObject.getJSONArray("cit");
      for(int i=0;i<city.length();i++)
      {
            String cities = (String) city.get(i);
            Log.i("All Cities",cities);
            kota.add(cities);
      }
share|improve this answer
add comment

Do it like this

    JSONArray json = jParser.getJSONFromUrl(URL);

JSONObject c = json.getJSONObject(0);

JSONArray city  = c.getJSONArray("cit");    

for (int j=0; j < city.length(); j++){

    String kot = cit.get(j);

    kota.add(kot);
}
share|improve this answer
    
Mere code is not an answer. Please explain to OP what's wrong with his code. –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Oct 14 '13 at 5:10
add comment
Object obj = yourJsonResult;
JSONObject jsonObject = (JSONObject) obj;
JSONArray msg = (JSONArray) jsonObject.get("cit");
Iterator<String> iterator = msg.iterator();

while (iterator.hasNext()) {
    System.out.println(iterator.next());
}
share|improve this answer
add comment

you can do it as following code this is tested

String jsonSource="{'cit': ['ALL','Aceh Barat','Aceh Besar','Aceh Jaya','Aceh Selatan','Aceh Tengah','Aceh Timur','Aceh Utara']}";
    try {
        JSONObject jsonObject=new JSONObject(jsonSource);
        JSONArray jsonArray=jsonObject.getJSONArray("cit");         
        int length = jsonArray.length();
        String [] cities = new String [length];
        if (length > 0) {
            for (int i = 0; i < length; i++) {
                cities[i] = jsonArray.getString(i);
            }
        }
        //to check we can print our string array
        for (int i = 0; i < cities.length; i++) {
            Log.d("JSON parsing ",cities[i]);               
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
share|improve this answer
    
Mere code is not an answer. Please explain to OP what's wrong with his code. –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Oct 14 '13 at 5:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.