GSON投掷“预计BEGIN_OBJECT,但是是STRING [英] GSON throwing “Expected BEGIN_OBJECT but was STRING
问题描述
这是Json Object
[
{
UserId:demouser1,
类别:[
{
CatagoryName:娱乐,
Persent:25
},
{
CatagoryName:Household,
Persent:25
},
{
CatagoryName:电影,
Persent:25
},
{
CatagoryName:Misc,
Persent:25
}
],
RequestId:null,
ResponseStatus:false,
Token:null
}
]
使用以下方法解析上述Json
public class CategoryEntity {
private String CatagoryName;
私人字符串Persent;
public String getCatagoryName(){
return CatagoryName;
}
public void setCatagoryName(String catagoryName){
CatagoryName = catagoryName;
}
public String getPersent(){
返回Persent;
}
public void setPersent(String persent){
Persent = persent;
}
}
import java.util.List;
公共类实体{
private String UserId;
public String getUserId(){
return UserId;
}
public void setUserId(String userId){
UserId = userId;
}
公共列表< CategoryEntity> getListCatagories(){
返回ListCatagories;
}
public void setListCatagories(List< CategoryEntity> listPMMCatagories){
ListCatagories = listPMMCatagories;
}
public String getRequestId(){
return RequestId;
}
public void setRequestId(String requestId){
RequestId = requestId;
}
public boolean isResponseStatus(){
return ResponseStatus;
}
public void setResponseStatus(boolean responseStatus){
ResponseStatus = responseStatus;
}
私人列表< CategoryEntity> ListCatagories;
私人字符串RequestId;
私人字符串令牌;
public String getToken(){
return Token;
}
public void setToken(String token){
Token = token;
}
私有布尔型ResponseStatus;
}
和
以下方法将json对象到相应的对象
Gson gson = new Gson();
JsonPrimitive listCatagoriesElement = element.getAsJsonPrimitive();
System.out.println(listCatagoriesElement.getAsString()>>+ listCatagoriesElement.getAsString());
$ b $ sysout print:listCatagoriesElement.getAsString()>> [{UserId:user1,ListCatagories:[{CatagoryName:Entertainment,Persent },{ CatagoryName : 家庭 Persent: 25},{ CatagoryName: 电影, Persent: 25},{ CatagoryName: 杂项, Persent :25}],RequestId:null,ResponseStatus:false,Token:null}]
实体实体= gson.fromJson(listCatagoriesElement,Entity.class);
任何想法我应该如何解决它?
感谢!
您的类 CategoryEntity
是正确的,你的类实体
,属性 ListCatagories
应该被称为类别
匹配JSON中的名称!
除此之外,为了解析JSON,你最好做这样的事情:
Gson gson = new Gson();
Type listType = new TypeToken< List< Entity>>(){} .getType();
列表< Entity> entities = gson.fromJson(yourJsonString,listType);
因此,您将有一个 List
,其中包含只需一个实体
对象,并且您可以通过以下方式访问这些值:
String catagoryNameI = entities.get(0).getCatagories()。get(i).getCatagoryName();
String persentI = entities.get(0).getCatagories()。get(i).getPersent();
您必须这么做,因为您的整个JSON响应是一个数组,由 [...]
,因此您需要将它解析为 List
...
This is Json Object
[
{
"UserId":"demouser1",
"Catagories":[
{
"CatagoryName":"Entertainment",
"Persent":"25"
},
{
"CatagoryName":"Household",
"Persent":"25"
},
{
"CatagoryName":"Movie",
"Persent":"25"
},
{
"CatagoryName":"Misc",
"Persent":"25"
}
],
"RequestId":null,
"ResponseStatus":false,
"Token":null
}
]
Used The Following approach to parse the above Json
public class CategoryEntity {
private String CatagoryName;
private String Persent;
public String getCatagoryName() {
return CatagoryName;
}
public void setCatagoryName(String catagoryName) {
CatagoryName = catagoryName;
}
public String getPersent() {
return Persent;
}
public void setPersent(String persent) {
Persent = persent;
}
}
import java.util.List;
public class Entity {
private String UserId;
public String getUserId() {
return UserId;
}
public void setUserId(String userId) {
UserId = userId;
}
public List<CategoryEntity> getListCatagories() {
return ListCatagories;
}
public void setListCatagories(List<CategoryEntity> listPMMCatagories) {
ListCatagories = listPMMCatagories;
}
public String getRequestId() {
return RequestId;
}
public void setRequestId(String requestId) {
RequestId = requestId;
}
public boolean isResponseStatus() {
return ResponseStatus;
}
public void setResponseStatus(boolean responseStatus) {
ResponseStatus = responseStatus;
}
private List<CategoryEntity> ListCatagories;
private String RequestId;
private String Token;
public String getToken() {
return Token;
}
public void setToken(String token) {
Token = token;
}
private boolean ResponseStatus;
}
And Following approach to convert the json object to corresponding object
Gson gson =new Gson();
JsonPrimitive listCatagoriesElement= element.getAsJsonPrimitive();
System.out.println("listCatagoriesElement.getAsString()>>"+listCatagoriesElement.getAsString());
sysout prints: listCatagoriesElement.getAsString()>>[{"UserId":"user1","ListCatagories":[{"CatagoryName":"Entertainment","Persent":"25"},{"CatagoryName":"Household","Persent":"25"},{"CatagoryName":"Movie","Persent":"25"},{"CatagoryName":"Misc","Persent":"25"}],"RequestId":null,"ResponseStatus":false,"Token":null}]
Entity entity = gson.fromJson(listCatagoriesElement, Entity.class);
Any ideas how should I fix it?
Thanks!
Your class CategoryEntity
is correct, but in your class Entity
, the attribute ListCatagories
should be called Catagories
to match the name in the JSON!
Apart from that, in order to parse the JSON you'd better do something like this:
Gson gson = new Gson();
Type listType = new TypeToken<List<Entity>>() {}.getType();
List<Entity> entities = gson.fromJson(yourJsonString, listType);
So you'll have a List
containing just one Entity
object, and you can access the values just with:
String catagoryNameI = entities.get(0).getCatagories().get(i).getCatagoryName();
String persentI = entities.get(0).getCatagories().get(i).getPersent();
You have to do this because your whole JSON response is an array, surrounded by [ ... ]
, so you need to parse it into some List
...
这篇关于GSON投掷“预计BEGIN_OBJECT,但是是STRING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!