android JSON文件解析为类对象 出现以下错误,求解答。
Causedby:java.lang.IllegalStateException:ExpectedBEGIN_OBJECTbutwasSTRINGatline1colum...
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 123
JSON文件:
{
"Data": {
"uid": 19880890,
"token": "d8dc52ba46e84685ab5549c01764dd9b",
"name": "benlixmu@gmail.com"
},
"Detail": "登入成功",
"Return": "0"
}
类对象
public class LoginData {
public Data Data;
public String Detail;
public string Return;
public static class Data {
public String name;
private String token;
public int uid;
}
}
解析语句:
LoginData loginData = json.fromJson(jsonData, LoginData.class);
jsonDate就是获得的JSON文件内容
public string Return;改为char型就OK了。 展开
JSON文件:
{
"Data": {
"uid": 19880890,
"token": "d8dc52ba46e84685ab5549c01764dd9b",
"name": "benlixmu@gmail.com"
},
"Detail": "登入成功",
"Return": "0"
}
类对象
public class LoginData {
public Data Data;
public String Detail;
public string Return;
public static class Data {
public String name;
private String token;
public int uid;
}
}
解析语句:
LoginData loginData = json.fromJson(jsonData, LoginData.class);
jsonDate就是获得的JSON文件内容
public string Return;改为char型就OK了。 展开
2个回答
展开全部
你要定义类的get和set方法,然后属性名字要用小写,其次return不要用,这是java关键字
public class LoginData {
private Data data;
private String detail;
private String retur;
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getRetur() {
return retur;
}
public void setRetur(String retur) {
this.retur = retur;
}
}
public class Data {
private int uid;
private String name;
private String token;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
}
测试类
public class Test {
public static void main(String[] args) {
String json="{\"data\":{\"uid\":19880890,\"token\":\"d8dc52ba46e84685ab5549c01764dd9b\",\"name\": \"benlixmu@gmail.com\"}, \"detail\": \"登入成功\",\"retur\": \"0\"}";
JSONObject object = JSONObject.fromObject(json);
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("data", Data.class);
LoginData loginData = (LoginData) JSONObject.toBean(object, LoginData.class,classMap);
System.err.println(loginData.getData().getName());
}
}
public class LoginData {
private Data data;
private String detail;
private String retur;
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getRetur() {
return retur;
}
public void setRetur(String retur) {
this.retur = retur;
}
}
public class Data {
private int uid;
private String name;
private String token;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
}
测试类
public class Test {
public static void main(String[] args) {
String json="{\"data\":{\"uid\":19880890,\"token\":\"d8dc52ba46e84685ab5549c01764dd9b\",\"name\": \"benlixmu@gmail.com\"}, \"detail\": \"登入成功\",\"retur\": \"0\"}";
JSONObject object = JSONObject.fromObject(json);
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("data", Data.class);
LoginData loginData = (LoginData) JSONObject.toBean(object, LoginData.class,classMap);
System.err.println(loginData.getData().getName());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询