解析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();
}