Java解析json数据
json格式如下:{"response":{"data":[{"address":"南京市游乐园","province":"江苏","district":"玄武区","city":"南京"}]},"status":"ok"}
希望得到结果是: 江苏 南京 玄武区 南京市游乐园 --------------------编程问答-------------------- 利用gson,i去网上下一个
这里是我的例子
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();
}
}
--------------------编程问答-------------------- 楼上截取这种方式感觉不是很好,以及用jsonObjec实现了,不过还是感谢 --------------------编程问答--------------------
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 = 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"));
}
你的封装太麻烦了!!! --------------------编程问答--------------------
+++顶一下 --------------------编程问答-------------------- 用JACKSON吧!通用的 --------------------编程问答-------------------- 推荐楼主用Jquery最好啦! --------------------编程问答-------------------- 嗯,jquery多简单 --------------------编程问答--------------------
离题了
Java解析json数据 --------------------编程问答-------------------- 真难搞,下个资源真难 --------------------编程问答--------------------
这个好,顶一下 ++++ --------------------编程问答-------------------- 你是页面用 还是服务器端用 服务器端用 推荐用 json.me --------------------编程问答-------------------- String str="......";
JSONObject json=new JSONObject().fromObject(str);
JSONArray arr=json.getJSONArray("response");
String address=null;
for(int i=0;i<arr.size();i++) {
address=(String) arr.getJSONObject(i).get("address");
}
system.out.print(address);
就这样 --------------------编程问答--------------------
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);
}
}
江苏
南京
玄武区
南京市游乐园
--------------------编程问答-------------------- google的gson和json-lib都可以 --------------------编程问答-------------------- 一直都是js自己封裝數據。。。。 --------------------编程问答-------------------- /**
* toJSON&fromJSON
*
* @author jiansheng.xujs
*/
public class JacksonHelper implements Serializable {
private static final long serialVersionUID = 977280939870035869L;
private static ObjectMapper toJSONMapper = new ObjectMapper();
private static ObjectMapper fromJSONMapper = new ObjectMapper();
static {
fromJSONMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public static String toJSON(Object obj) {
ObjectMapper mapper = toJSONMapper;
StringWriter writer = new StringWriter();
try {
mapper.writeValue(writer, obj);
} catch (JsonGenerationException e) {
throw new RuntimeException(e);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return writer.toString();
}
public static void toJSON(Object obj, OutputStream stream, String charset) {
ObjectMapper mapper = toJSONMapper;
try {
OutputStreamWriter writer = new OutputStreamWriter(stream, charset);
mapper.writeValue(writer, obj);
} catch (JsonGenerationException e) {
throw new RuntimeException(e);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static <T> T fromJSON(String json, Class<T> clazz) {
ObjectMapper mapper = fromJSONMapper;
try {
return mapper.readValue(json, clazz);
} catch (JsonParseException e) {
throw new RuntimeException(e);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static <T> T fromJSON(InputStream json, Class<T> clazz) {
ObjectMapper mapper = fromJSONMapper;
try {
return mapper.readValue(json, clazz);
} catch (JsonParseException e) {
throw new RuntimeException(e);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
//##################################
CancelLogisticsOrderReq req = JacksonHelper.fromJSON(params,
CancelLogisticsOrderReq.class);
json 转换类 对象 供参考! --------------------编程问答-------------------- 去找找jsme的json包 --------------------编程问答-------------------- google的gson和json-lib都可以----- 这个可以的,都用过,不过还是google的gson更好用些 --------------------编程问答--------------------
以前遇到这个问题的时候专门学的,然后发了个帖子留着以后备用,希望能对你有用。。
http://bbs.csdn.net/topics/390337723 --------------------编程问答-------------------- 3楼+1 --------------------编程问答-------------------- google的gson封装了非常强大的方法,使用很方便。 --------------------编程问答--------------------
厉害,马克一下,留作以后用!
补充:Java , J2ME