JAVA Json 嵌套解析 200
如下:{"user_get_response":{"user":{"buyer_credit":{"good_num":0,"level":0,"score":0,"to...
如下:
{"user_get_response":{"user":{"buyer_credit":{"good_num":0,"level":0,"score":0,"total_num":0},"created":"2009-09-30 13:15:54","last_visit":"2009-12-22 13:31:58","location":{},"nick":"sandbox_c_23","seller_credit":{"good_num":0,"level":0,"score":0,"total_num":0}}}}
用java json如何解析这段?
不嵌套的容易,但嵌套就不会了 望高手来解 谢谢您了。 展开
{"user_get_response":{"user":{"buyer_credit":{"good_num":0,"level":0,"score":0,"total_num":0},"created":"2009-09-30 13:15:54","last_visit":"2009-12-22 13:31:58","location":{},"nick":"sandbox_c_23","seller_credit":{"good_num":0,"level":0,"score":0,"total_num":0}}}}
用java json如何解析这段?
不嵌套的容易,但嵌套就不会了 望高手来解 谢谢您了。 展开
4个回答
展开全部
首先楼上说的部分是对的。那么我先假设你已经获得了这个字符串,将之定义为jsonString.
JSONObject jsonObject = new JSONObject(jsonString);
然后,根据嵌套的种类,可以确定下一步如何取值。根据你的情况来看。应该是:
String user_get_response = jsonObject.getString("user_get_response");
JSONObject user_get_response_obj = new JSONObject(user_get_response);
这是针对第一层嵌套的。
一层一层的取下去就好。如果是数组类型的,就用JSONArray来接收。
JSONArray jsonArray = new JSONArray(jsonObject.getString("xxx"));
总之就是由外到内去解析就好
JSONObject jsonObject = new JSONObject(jsonString);
然后,根据嵌套的种类,可以确定下一步如何取值。根据你的情况来看。应该是:
String user_get_response = jsonObject.getString("user_get_response");
JSONObject user_get_response_obj = new JSONObject(user_get_response);
这是针对第一层嵌套的。
一层一层的取下去就好。如果是数组类型的,就用JSONArray来接收。
JSONArray jsonArray = new JSONArray(jsonObject.getString("xxx"));
总之就是由外到内去解析就好
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可以使用google-gson包(很多地方都有下载)。下面的程序已经测试没有问题:
import java.util.*;
import java.lang.reflect.Type;
import com.google.gson.*;
import com.google.gson.reflect.*;
class Address {
String street;
String city;
String province;
String country;
public Address() {};
public String toString() {return street;}
}
class CreditInfo {
int good_num;
int level;
int score;
int total_num;
public CreditInfo() {};
public String toString() {
return String.format("[good_num: %1$s, level: %2$s, score: %3$s, total_num: %4$s]", good_num, level, score, total_num);
}
}
class User {
CreditInfo buyer_credit;
Date created;
Date last_visit;
Address location;
String nick;
CreditInfo seller_credit;
public User() {buyer_credit=new CreditInfo(); seller_credit=new CreditInfo();}
public String toString() {
return String.format("buyer_credit: %1$s, \ncreated: %2$s, \nlast_visit: %3$s, \nlocation: %4$s, \nnick: %5$s, \nseller_credit: %6$s",
buyer_credit,
created,
last_visit,
location,
nick,
seller_credit);
}
}
class UserResponse {
User user;
public UserResponse() {}
public String toString() {return "user = " + user;}
}
class Response {
UserResponse user_get_response;
public Response() {}
public String toString() {return user_get_response.toString();}
}
public class Test {
public static void main(String[] args) {
String s = "{\"user_get_response\":{\"user\":{\"buyer_credit\":{\"good_num\":0,\"level\":0,\"score\":0,\"total_num\":0},\"created\":\"2009-09-30 13:15:54\",\"last_visit\":\"2009-12-22 13:31:58\",\"location\":{},\"nick\":\"sandbox_c_23\",\"seller_credit\":{\"good_num\":0,\"level\":0,\"score\":0,\"total_num\":0}}}}";
Gson gson = new Gson();
Response r = gson.fromJson(s, Response.class);
System.out.println(r);
}
}
import java.util.*;
import java.lang.reflect.Type;
import com.google.gson.*;
import com.google.gson.reflect.*;
class Address {
String street;
String city;
String province;
String country;
public Address() {};
public String toString() {return street;}
}
class CreditInfo {
int good_num;
int level;
int score;
int total_num;
public CreditInfo() {};
public String toString() {
return String.format("[good_num: %1$s, level: %2$s, score: %3$s, total_num: %4$s]", good_num, level, score, total_num);
}
}
class User {
CreditInfo buyer_credit;
Date created;
Date last_visit;
Address location;
String nick;
CreditInfo seller_credit;
public User() {buyer_credit=new CreditInfo(); seller_credit=new CreditInfo();}
public String toString() {
return String.format("buyer_credit: %1$s, \ncreated: %2$s, \nlast_visit: %3$s, \nlocation: %4$s, \nnick: %5$s, \nseller_credit: %6$s",
buyer_credit,
created,
last_visit,
location,
nick,
seller_credit);
}
}
class UserResponse {
User user;
public UserResponse() {}
public String toString() {return "user = " + user;}
}
class Response {
UserResponse user_get_response;
public Response() {}
public String toString() {return user_get_response.toString();}
}
public class Test {
public static void main(String[] args) {
String s = "{\"user_get_response\":{\"user\":{\"buyer_credit\":{\"good_num\":0,\"level\":0,\"score\":0,\"total_num\":0},\"created\":\"2009-09-30 13:15:54\",\"last_visit\":\"2009-12-22 13:31:58\",\"location\":{},\"nick\":\"sandbox_c_23\",\"seller_credit\":{\"good_num\":0,\"level\":0,\"score\":0,\"total_num\":0}}}}";
Gson gson = new Gson();
Response r = gson.fromJson(s, Response.class);
System.out.println(r);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你想要自己写程序解析吗?像这种问题一般用栈,每次碰到{或者[压入一次栈,每次碰到}或者]弹出一次栈。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
{"user_get_response":{"user":{
"buyer_credit":{
"good_num":0,
"level":0,
"score":0,
"total_num":0
},
"created":"2009-09-30 13:15:54",
"last_visit":"2009-12-22 13:31:58",
"location":{},
"nick":"sandbox_c_23",
"seller_credit{
"good_num":0,
"level":0,
"score":0,
"total_num":0
}
}}}
String json = readJSONString(request);
JSONObject jsonObject = null;
String responseText = null;
String
try {
jsonObject = new JSONObject(json);
String user_get_response=jsonObject.getString("user_get_response");
JSONObject jsonObject_1=new JSONObject(user_get_response);
String user=jsonObject_1.getString("user");
JSONObject jsonObject_2=new JSONObject(user);
....
}
依次类推
"buyer_credit":{
"good_num":0,
"level":0,
"score":0,
"total_num":0
},
"created":"2009-09-30 13:15:54",
"last_visit":"2009-12-22 13:31:58",
"location":{},
"nick":"sandbox_c_23",
"seller_credit{
"good_num":0,
"level":0,
"score":0,
"total_num":0
}
}}}
String json = readJSONString(request);
JSONObject jsonObject = null;
String responseText = null;
String
try {
jsonObject = new JSONObject(json);
String user_get_response=jsonObject.getString("user_get_response");
JSONObject jsonObject_1=new JSONObject(user_get_response);
String user=jsonObject_1.getString("user");
JSONObject jsonObject_2=new JSONObject(user);
....
}
依次类推
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询