使用gson解析Json文件:MalformedJsonException
我想读取一个像JSON文件一样被格式化的字符串并将其打印在屏幕上。 因此我正在使用GSON-Library。使用gson解析Json文件:MalformedJsonException
每次我想要编译我得到一个线程异常“主” com.google.gson.JsonSyntaxException错误
我的代码看起来像这样
public class Test {
public static void main(String... args) throws Exception {
String json =
"{"
+ "'tag_name' : 'M mit Mbrunnen',"
+ "'tag_id' : 'de_bw_xx_mstall',"
+ "'tag_description': 'false',"
+ "'tag_latitude': '42.704895',"
+ "'tag_longitude': '10.652187',"
+ "'tag_description_f_a': 'Ein weiteres Highlight in H',"
+ "}";
// Now do the magic.
Data data = new Gson().fromJson(json, Data.class);
// Show it.
System.out.println(data);
}
}
class Data {
private String tag_name;
private String tag_id;
private String tag_description;
private String tag_latitude;
private String tag_longitude;
private String tag_descrption_f_a;
public String getName() { return tag_name; }
public String getId() { return tag_id; }
public String getDescription() { return tag_description; }
public String getLatitude() { return tag_latitude; }
public String getLongitude() { return tag_longitude; }
public String getDescriptionVoice() { return tag_descrption_f_a; }
public void setName(String name) { this.tag_name = name; }
public void setId(String id) { this.tag_id = id; }
public void setDescription(String description) { this.tag_description = description; }
public void setLatitude(String latitude) { this.tag_latitude = latitude; }
public void setLongitude(String longitude) { this.tag_longitude = longitude; }
public void setDescriptionVoice(String descriptionVoice) { this.tag_descrption_f_a = descriptionVoice; }
public String toString() {
return String.format("%s,%s,%s,%s,%s,%s", tag_name, tag_id, tag_description, tag_latitude, tag_longitude, tag_descrption_f_a);
}
}
我得到的错误:
Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 221 at com.google.gson.Gson.fromJson(Gson.java:769) at com.google.gson.Gson.fromJson(Gson.java:721) at com.google.gson.Gson.fromJson(Gson.java:670) at com.google.gson.Gson.fromJson(Gson.java:642) at Test.main(Test.java:19) Caused by: com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 221 at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1298) at com.google.gson.stream.JsonReader.nextInObject(JsonReader.java:739) at com.google.gson.stream.JsonReader.peek(JsonReader.java:382) at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:349) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:169) at com.google.gson.Gson.fromJson(Gson.java:755) ... 4 more
所以错误发生在这里:
// Now do the magic. Data data = new Gson().fromJson(json, Data.class);
我想我的JSON数据提供的格式不正确。
我想你的JSON中没有很好地形成
它应该是这样的
"{
"tag_name": "M mit Mbrunnen", // always use double quotes, single quote is not a valid json
"tag_id": "de_bw_xx_mstall",
"tag_description": "false",
"tag_latitude": "42.704895",
"tag_longitude": "10.652187",
"tag_description_f_a": "Ein weiteres Highlight in H" // extra comma removed from here
}"
您可以验证您的JSON这里http://jsonlint.com/
感谢您有用的答案和验证页! 当然,你总是用“而不是”,但因为JSON实际上是一个字符串,所以'是正确的。 错误在这里: +''tag_description_f_a':'Ein weiteres H' - > RCK69 2012-04-09 13:48:31