41112

parsing Json File with gson: MalformedJsonException

Question:

I want to read a string which is formated like a JSON-File and print it on screen. Therefore I am useing the GSON-Library.

Everytime I want to compile I get an <strong>Exception in thread "main" com.google.gson.JsonSyntaxException</strong> Error

My Code looks like this

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

I am getting the error:

<blockquote>

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

</blockquote>

so the error happens here:

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

I think my JSON-Data is provided in a wrong format.

Answer1:

i think your JSON in not well formed

it should be something like this

"{ "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 }"

you can validate your JSON here <a href="http://jsonlint.com/" rel="nofollow">http://jsonlint.com/</a>

Recommend

  • Using an array for autofilter criteria
  • Entity Framework unable to delete database, database in use
  • Deleting a widget from QTableView
  • Arduino making decision according to a packet received from serial port
  • RegistryKey.GetSubKeyNames returns names that are not in that subkey!
  • to implement a spinner in angular2+
  • MVC - @Html.CheckBoxFor
  • Deleting empty Series out of Graph (with VBA)
  • How to populate html table with info from list in django
  • Bound DataGridViewCheckBoxColumn not updating binding source
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • Calling Rcpp function with Xptr and Function - only xptr case works
  • Unable to connect to OnVif enabled camera using C#
  • jquery code not working without breakpoint
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • Detecting # in Scheme list
  • Android onKey w/ virtual keyboard
  • Adding Object or changing its parameter programmatically in C# / ASP.Net
  • encoding issues with content in response from HttpWebRequest
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • Validate jQuery plugin, field not required
  • Setting the run time properties on SpringApplicationBuilder()
  • Calculating ratio of reciprocated ties for each node in igraph
  • How to disable all widgets inside Panel or inside Composite?
  • Extract zip entries to another Zip file
  • Simulate click Geckofx vb,net
  • Groovy: Unexpected token “:”
  • How to have background script and something similar to a default popup?
  • d3 v4 drag and drop with TypeScript
  • jQuery show() function is not executed in Safari if submit handler returns true
  • RectangularRangeIndicator format like triangular using dojo
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • Possible to stop flickering java tooltip in heavyweight mode?
  • How to model a transition system with SPIN
  • Cant find why the layout is getting smaller
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs