您的位置:首页 > 移动开发 > Android开发

Android JSON解析

2016-02-14 16:02 260 查看
String json = "{" +
"\"languages\" : [" +
"{\"id\":1,\"ide\":\"Eclipse\",\"name\":\"Java\"}," +
"{\"id\":2,\"ide\":\"XCode\",\"name\":\"Swift\"}," +
"{\"id\":3,\"ide\":\"Visual Studio\",\"name\":\"C#\"}" +
"]," +
"\"cat\" : \"it\"" +
"}";
try {
JSONObject root = new JSONObject(json);
String all = "";
String cat = "cat = " + root.getString("cat") + "\n";
all = all + cat;
JSONArray array = root.getJSONArray("languages");
for (int i = 0;i < array.length();i++) {
JSONObject lan = array.getJSONObject(i);
String sep = "-------------------------" + "\n";
String id = "id = " + lan.getInt("id") + "\n";
String name = "name = " + lan.getString("name") + "\n";
String ide = "ide=" + lan.getString("ide") + "\n";
all = all + sep + id + name + ide;
}
tv = (TextView)findViewById(R.id.all);
tv.setText(all);
} catch(JSONException e) {
Toast.makeText(MainActivity.this,"error",Toast.LENGTH_LONG).show();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: