我使用Gson lib将json stirng反序列化为Java对象,但是发生了错误。
下面是json字符串:
{
"pageInfo": [
{
"detail": "{\"key1\":\"value1\",\"key2\":\"value2\"}",
"id": 4,
"type": 0
}]
}
和java类:
public java.util.List pageInfo;
class CreditDetailItem {
public String id;
public int type;
public String detail;
}
然后抛出一个异常:预期BEGIN_OBJECT但在'细节'上是STRING 属性。
有没有人可以帮助我?
答案 0 :(得分:0)
是的,JSON与Java代码不匹配。以下是在JAVA中进行此操作的推荐方法。
class JSONWrapper {
java.util.List<PageInfo> pageInfo = new java.util.ArrayList<PageInfo>();
public void setPageInfo(java.util.List<PageInfo> pageInfo) {
this.pageInfo = pageInfo;
}
public java.util.List<PageInfo> getPageInfo() {
return pageInfo;
}
}
class PageInfo {
String id;
int type;
String detail;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
答案 1 :(得分:0)
假设这是您格式化的E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.Restarter.getActivities
W/dalvikvm: VFY: unable to resolve instanceof 2042 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/Restarter;
D/dalvikvm: VFY: replacing opcode 0x20 at 0x006f
I/dalvikvm: Could not find method android.app.Activity.getColor, referenced from method catalog.raulfmiranda.com.catalog.ui.adapter.CategoriaRecyclerAdapter.onBindViewHolder
W/dalvikvm: VFY: unable to resolve virtual method 58: Landroid/app/Activity;.getColor (I)I
D/dalvikvm: VFY: replacing opcode 0x6e at 0x0058
I/dalvikvm: Could not find method android.app.Activity.getColor, referenced from method catalog.raulfmiranda.com.catalog.ui.adapter.CategoriaRecyclerAdapter.onBindViewHolder
W/dalvikvm: VFY: unable to resolve virtual method 58: Landroid/app/Activity;.getColor (I)I
D/dalvikvm: VFY: replacing opcode 0x6e at 0x0067
D/AndroidRuntime: Shutting down VM
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41a7a8c8)
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NoSuchMethodError: android.app.Activity.getColor
at catalog.raulfmiranda.com.catalog.ui.adapter.CategoriaRecyclerAdapter.onBindViewHolder(CategoriaRecyclerAdapter.java:52)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5825)
数据。
JSON
试试这个{
"pageInfo": [
{
"detail": {
"key1": "value1",
"key2": "value2"
},
"id": 4,
"type": 0
}]
}
课程,
POJO
解析public class ParsedData {
List<CreditDetailItem> pageInfo = new ArrayList<CreditDetailItem>();
class CreditDetailItem {
public String id;
public int type;
public Detail detail;
//getters and setters
}
public class Detail {
public String key1;
public String key2;
//getters and setters
}
}
,
JSON
答案 2 :(得分:0)
检查这个,它为我工作。
如果不存在,则在build.gradle中添加依赖项:
compile("com.google.code.gson:gson:2.8.0")
这是你的JSONObject:
{
"pageInfo": [
{
"detail": "{\"key1\":\"value1\",\"key2\":\"value2\"}",
"id": 4,
"type": 0
}
]
}
这是您的pageinfo列表模型类:
public class MyModelList {
public List<MyModel> pageInfo = new ArrayList();
}
这适用于您的pageinfo列表项模型类:
public class MyModel {
public String id;
public int type;
public String detail;
}
这是您可以为单独的项目或列表模型检查gson到json和副版本转换的方法:
private void myGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
//Convert to java objects
MyModel mModel = gson.fromJson("{\n" +
" \"detail\": \"{\\\"key1\\\":\\\"value1\\\",\\\"key2\\\":\\\"value2\\\"}\",\n" +
" \"id\": 4,\n" +
" \"type\": 0\n" +
" }"
, MyModel.class);
MyModelList mModelList = gson.fromJson("{\n" +
" \"pageInfo\": [\n" +
" {\n" +
" \"detail\": \"{\\\"key1\\\":\\\"value1\\\",\\\"key2\\\":\\\"value2\\\"}\",\n" +
" \"id\": 4,\n" +
" \"type\": 0\n" +
" }" +
"]\n" +
" }"
, MyModelList.class);
Log.e("TAG", mModel.toString());
Log.e("TAG", mModelList.toString());
//Convert to JSON
String sModelList = gson.toJson(mModelList);
String sModel = gson.toJson(mModel);
}
答案 3 :(得分:-1)
public class CreditDetailItem {
@SerializedName(“pageInfo”)
@expose
public List pageInfo = new ArrayList();
公共类PageInfo {
@SerializedName(“detail”)
@expose
public String detail;
@SerializedName(“id”)
@expose
public Integer id;
@SerializedName(“type”)
@expose
public Integer type;
}
}