I am trying to parse URL to get JSON response and particular values from that response. I dont have a sample code. Please give me a simple solution. Below I have posted my URL and Response. I want to GET "School", Name" and Result values.
http://sample.com/login/username/ <username> /password <password>?
{
"response":{
"School":"SBOA",
"Name":"Anitha",
"Class":"Tenth",
},
"Result":"Good",
}
My Code :
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView output = (TextView) findViewById(R.id.textView1);
String strJson="URL";
String data = "";
System.out.println(strJson);
try {
JSONObject jsonRootObject = new JSONObject(strJson);
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("response");
System.out.println(jsonRootObject);
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
// int id = Integer.parseInt(jsonObject.optString("id").toString());
String name = jsonObject.optString("School").toString();
// float salary = Float.parseFloat(jsonObject.optString("salary").toString());
// data += "Node"+i+" : \n id= "+ id +" \n Name= "+ name +" \n Salary= "+ salary +" \n ";*/
}
//output.setText(data);
} catch (JSONException e) {e.printStackTrace();}
}
}