使用gson:MalformedJsonException解析Json File

我想读一个像JSON-File一样的字符串并在屏幕上打印。 因此我使用的是GSON-Library。

每次我想编译时,我在线程“main”com.google.gson.JsonSyntaxException错误中得到一个exception

我的代码看起来像这样

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); } } 

我收到错误:

线程“main”中的exceptioncom.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:com.google.gson.Gson.fromJson(Gson.java:769)第1行第221列的预期名称.google.gson.Gson.fromJson(Gson.java:721)位于com.google.gson.Gson.fromJson(Gson.java:670)的com.google.gson.Gson.fromJson(Gson.java:642)at Test.main(Test.java:19)引起:com.google.gson.stream.MalformedJsonException:com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1298)第1行第221列的预期名称at Com.google.gson.stream.JsonReader.nextInObject(JsonReader.java:739)位于com.google.gson.stream.JsonReader.peek(JsonReader.java:382)com.google.gson.stream.JsonReader.hasNext( JsonReader.java:349)com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:169)at com.google.gson.Gson.fromJson(Gson.java:755)… 4更多

所以错误发生在这里:

  // Now do the magic. Data data = new Gson().fromJson(json, Data.class); 

我认为我的JSON-Data以错误的格式提供。

我认为你的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 }" 

你可以在这里validation你的JSON http://jsonlint.com/