Anuj Gajbhiye Anuj Gajbhiye - 1 year ago 48
JSON Question

How should I parse this kind of JSON?

I'm new to JSON parsing. It would be a great help if anyone would help me with parsing this kind of json array in Android.
Thank you

{
"response": 200,
"department": [
"Information Technology"
],
"subject": [
"ads(th)"
],
"professional": [
"cg(th)",
"cg(lab)"
],
"semester": [
"3A",
"5A",
"5A"
]
}

Answer Source

This is ur response:

{
  "response": 200,
  "department": [
    "Information Technology"
  ],
  "subject": [
    "ads(th)"
  ],
  "professional": [
    "cg(th)",
    "cg(lab)"
  ],
  "semester": [
    "3A",
    "5A",
    "5A"
  ]
}

U can do like this

String responseString="" //this string is ur web service response
try {
        //JSON is the JSON code above

        JSONObject jsonResponse = new JSONObject(responseString);
        JSONArray department = jsonResponse.getJSONArray("department");
        String hey = department.toString();

JSONArray subject = jsonResponse.getJSONArray("subject");
        String sub = subject.toString();

JSONArray  professional= jsonResponse.getJSONArray("professional");
        String pro = professional.toString();
//like this u can parse other JsonArray

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

After getting those values in jsonarray u want to display it in spinner than u can do like this

ArrayList<String> listdata = new ArrayList<String>();  
if (professional != null) { 
   for (int i=0;i<professional.length();i++){ 
    listdata.add(professional.getString(i));
   } 
} 

For Display into spinner

Spinner spinner = (Spinner) findViewById(R.id.SpinnerSpcial);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listdata);//Pass list data of Profession
spinner.setAdapter(adapter);

Notes: You can make custom adapter also by extending BaseAdapter or ArrayAdapter for Spinner.

Hope this will help u ... if u have any questions u can ask

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download