0

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.

4
  • 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.) Commented Oct 14, 2013 at 4:36
  • 1
    use this String kot=city.get(j) Commented Oct 14, 2013 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 Commented Oct 14, 2013 at 4:42
  • @AoyamaNanami the spinner will show the above, because that is what is there in the json. What's wrong with it ? Commented Oct 14, 2013 at 5:19

4 Answers 4

2
"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);
      }
Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

1 Comment

Mere code is not an answer. Please explain to OP what's wrong with his code.
0
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());
}

Comments

0

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();
    }

1 Comment

Mere code is not an answer. Please explain to OP what's wrong with his code.

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.