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

android studio jason文件解析基础

2016-07-23 11:21 483 查看
解析jason中非数组元素:

tv=(TextView)findViewById(R.id.text);
String jsonstr="{\"name\":\"ailsa\",\"age\":22,\"arr\":[1,2,\"hello\"]}";
try {
JSONObject jsonObject=new JSONObject(jsonstr);
String name=jsonObject.getString("name");
tv.setText(name);
} catch (JSONException e) {
e.printStackTrace();
}

解析jason中数组元素:(如何这里不能正常输出,可以尝试重启studio)

tv=(TextView)findViewById(R.id.text);
String jsonstr="{\"name\":\"ailsa\",\"age\":22,\"arr\":[1,2,\"hello\"]}";
try {
JSONObject jsonObject=new JSONObject(jsonstr);
JSONArray arr=jsonObject.getJSONArray("arr");
int value=arr.getInt(0);

System.out.println(value);
} catch (JSONException e) {
e.printStackTrace();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: