我的 Json 字符串看起来像这样:
{
"Status": "Success",
"ErrorText": "",
"GetMySizeResult": {
"Chest": "43",
"Shoulder": "33",
"NeckFront": "32",
"NeckBack": "2",
"Waist": "3",
"Abdomen": "3",
"Hip": "4",
"Length": "4",
"SlitLength": "4",
"ArmHole": "4",
"SleeveLengthShort": "4",
"SleeveLengthLong": "4",
"SleeveRoundShort": "",
"SleeveRoundLong": "",
"WaistToAnkle": "",
"Thigh": "",
"Knee": "",
"Calf": "",
"Ankle": "",
"Instruction": "",
"Note": ""
}
}
我的代码如下:
@SuppressWarnings("unchecked")
public static <T> T JsonParseforReturnModel(T t, String response)
throws JsonSyntaxException, IOException, XmlPullParserException {
InputStream in = new ByteArrayInputStream(response.getBytes());
JsonReader reader;
reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
Gson gson = new Gson();
reader.beginObject();
reader.nextName();
reader.nextString();
reader.nextName();
reader.nextString();
reader.nextName();
reader.beginObject();
t = (T) gson.fromJson(reader, t.getClass());
reader.close();
return t;
}
但它包含错误:
Expected BEGIN_OBJECT but was NAME at line 1 column 61
请解决我的错误。提前致谢。抱歉我的英语不好。
最佳答案
在调用 gson.fromJson()
之前尝试删除 reader.beginObject()
。看起来 gson.fromJson()
需要 begin_object 标签,但它已经被消耗了。
关于java - 安卓GSON错误:Expected BEGIN_OBJECT but was NAME at line 1 column 61,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20698440/