json字符串使用java解析
上一篇 /
下一篇 2012-07-05 08:40:47
/ 个人分类:json
到官网上下载org.json,源码结构如下:
FromJson.java
package test;
import org.json.JSONArray;
import org.json.JSONObject;
public class FromJson {
public FromJson(){
String sTotalString = "{'customer': [{'website': 'http://www.c1.com', 'phone': 'c1', 'contact': 'c1', " +
"'name': 'c1'}], 'created_on': '2012-07-03 18:06:26', 'query_count': 13, 'now': '2012-07-03 21:42:02'}";
JSONObject json ;
try{
json = new JSONObject(sTotalString);
JSONArray results = json.getJSONArray("customer");
GetCustomer getCustomer = new GetCustomer();
JSONObject result = results.getJSONObject(0);
System.out.println(result.getString("website")+" "+result.getString("phone"));
getCustomer.setCustomerWebsite(result.getString("website"));
getCustomer.setCustomerPhone(result.getString("phone"));
getCustomer.setCustomerContact(result.getString("contact"));
getCustomer.setCustomerName(result.getString("name"));
System.out.println(json.getString("created_on"));
System.out.println(json.getInt("query_count")+"");
System.out.println(json.getString("now"));
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FromJson ss = new FromJson();
}
}
GetCustomer.java
package test;
public class GetCustomer {
private String customerWebsite;
private String customerPhone;
private String customerContact;
private String customerName;
public String getCustomerWebsite(){
return customerWebsite;
}
public void setCustomerWebsite(String customerWebsite){
this.customerWebsite = customerWebsite;
}
public String getCustomerPhone(){
return customerPhone;
}
public void setCustomerPhone(String customerPhone){
this.customerPhone = customerPhone;
}
public String getCustomerContact(){
return customerContact;
}
public void setCustomerContact(String customerContact){
this.customerContact = customerContact;
}
public String getCustomerName(){
return customerName;
}
public void setCustomerName(String customerName){
this.customerName = customerName;
}
}
显示结果:
http://www.c1.comc1
2012-07-03 18:06:26
13
2012-07-03 21:42:02
收藏
举报
TAG:
json