引入
import org.json.*;
1.简单json
{
"name":"小明",
"phone":"15556666333"
}
String str ="{\"name\":\"小明\",\"phone\":\"15556666333\"}";
JSONObject jsonstr=new JSONObject(str);
String name=jsonstr.getString("name");
String phone=jsonstr.getString("phone");
2.复杂json
[
{
"name":"小明",
"phone":"15556666333"
},
{
"name":"小猪",
"phone":"15556666444"
}
]
String str ="[{\"name\":\"小明\",\"phone\":\"15556666333\"},{\"name\":\"小猪\",\"phone\":\"15556666444\"}]";
JSONArray strarr = new JSONArray(str);
JSONObject jsonstr = rjsonArray.getJSONObject(0);
String name=jsonstr.getString("name");
JSONObject jsonstr1 = rjsonArray.getJSONObject(1);
String name2=jsonstr1.getString("name");