首页 > 代码库 > java 截取json
java 截取json
A:
public static void main(String[] args) {
// TODO Auto-generated method stub
String json = "{‘response‘:{‘data‘:[{‘address‘:‘南京市游乐园‘,‘province‘:‘江苏‘,‘district‘:‘玄武区‘,‘city‘:‘南京‘}]},‘status‘:‘ok‘}";
JSONObject jsonObject = JSONObject.fromObject(json);
String response = jsonObject.getString("response");
JSONObject jsonObject2 = JSONObject.fromObject(response);
JSONArray data = http://www.mamicode.com/jsonObject2.getJSONArray("data");
for (int i = 0; i < data.size(); i++) {
String s = data.getString(i);
JSONObject data2 = JSONObject.fromObject(s);
System.out.println(data2.getString("address"));
System.out.println(data2.getString("province"));
System.out.println(data2.getString("district"));
System.out.println(data2.getString("city"));
}
}
B:
public static void main(String[] args) {
String json = "{\"response\":{\"data\":[{\"address\":\"南京市游乐园\",\"province\":\"江苏\",\"district\":\"玄武区\",\"city\":\"南京\"}]},\"status\":\"ok\"}";
String str[] = json.replaceAll(
".*\"address\":\"(.*?)\".*\"province\":\"(.*?)\",.*\"district\":\"(.*?)\",.*\"city\":\"(.*?)\".*",
"$2,$4,$3,$1").split(",");
for (String s : str) {
System.out.println(s);
}
}
C:还有一个思想就是
public class Test {
public static void main(String[] args) {String date = "{‘response‘:{‘data‘:[{‘address‘:‘南京市游乐园‘,‘province‘:‘江苏‘,‘district‘:‘玄武区‘,‘city‘:‘南京‘}]},‘status‘:‘ok‘}";
date = date.substring(date.indexOf("[") + 1, date.indexOf("]"));
System.out.println(date);
Gson gson = new Gson();
Info info = gson.fromJson(date, Info.class);
System.out.println(info);//这里就根据info的信息取值就好了
}
}
class Info {
private String address;
private String province;
private String district;
private String city;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Info [address=" + address + ", province=" + province
+ ", district=" + district + ", city=" + city + "]";
}
public Info(String address, String province, String district, String city) {
super();
this.address = address;
this.province = province;
this.district = district;
this.city = city;
}
public Info() {
super();
}
}
java 截取json
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。