I have tried parsing JSON for the first time. In my JSON data there are multiple JSON objects in a single JSON array. The data sample:
{ “root”:[ {“Sc_we”:[ ]}, {“Sc_wesam”:[ {“head”:”Welcome page”}, {“color”:”Black”} ]} ] }
This is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
website = new URL(
"http://xxxxxxx");
InputStream in = website.openStream();
parseMovie(in);
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void parseMovie(InputStream json) throws IOException,
JSONException {
// TODO Auto-generated method stub
BufferedReader reader = new BufferedReader(new InputStreamReader(json));
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
while (line != null) {
sb.append(line);
line = reader.readLine();
}
reader.close();
System.out.println(sb);
JSONObject jobj = new JSONObject(sb.toString());
System.out.println("jsonobj:" + jobj);
JSONArray array = jobj.getJSONArray("root");
System.out
.println("jsonobject :+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
+ array);
}
I get the above JSON data, but I need the S_we value and Sc_wesam data.
How do I do this?
You can iterate over individual elements using your
JSONArray, and make some assumptions about your data: