我第一次尝试解析 JSON。在我的 JSON 数据中,单个 JSON 数组中有多个 JSON 对象。数据样本:
{ "root":[ {"Sc_we":[ ]}, {"Sc_wesam":[ {"head":"欢迎页面"}, {"color":"Black"} ]} ] }
这是我的代码:
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);
}
我得到了上面的 JSON 数据,但我需要S_we
值和Sc_wesam
数据。
我该怎么做呢?