Request Urgent Help : Exception in parsing using fromJson

4,299 views
Skip to first unread message

geeg

unread,
Jul 25, 2012, 5:05:21 PM7/25/12
to googl...@googlegroups.com
Hi All,

I might be doing something wrong. But i need to solve this error i am getting. I am getting an error when i parse/deserialize json that has white space in the value.

My json string representation is :

"{\"ip\":\"184.72.42.15\",\"city\":\"New York-744662523\"}]}"

I get error :

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 133

When i remove white space (replaced with + ) and convert the json to :


"{\"ip\":\"184.72.42.15\",\"city\":\"New+York-744662523\"}]}"

It works.

I am deserializing something like this :

List<HashMap<String,String>> roomsList = gson.fromJson(jsonString, new TypeToken<List< HashMap<String, String> >>() {}.getType());

Please tell me what is wrong here.

Brandon Mintern

unread,
Jul 25, 2012, 7:42:47 PM7/25/12
to googl...@googlegroups.com
The error message indicates an unterminated JsonObject (denoted by
{}). Perhaps when you removed whitespace, you also fixed a syntax
error in your JSON? It's hard to tell since you've included only a
small, syntactically-invalid snippet of the JSON that you're trying to
parse.
> --
> You received this message because you are subscribed to the Google Groups
> "google-gson" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-gson/-/zmxKtkEkl_QJ.
> To post to this group, send email to googl...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-gson...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.

Satyarth Negi

unread,
Jul 25, 2012, 7:55:27 PM7/25/12
to googl...@googlegroups.com
Hi Brandon,

Thanks for reply. This is what exactly i am trying to send but does not work due to white space in "New York"

msg = "{\"ip\":\"184.72.42.15\",\"game\":\"Game\",\"zone\":\"Blitz\",\"rooms\":[{\"id\":\"901\",\"left\":\"1\",\"maxplayers\":\"300\",\"players\":\"0\",\"cards_to_winners\":\"0.12\",\"cards\":\"0\",\"threshold\":\"1\",\"city\":\"New York-744662523\"}]}";

I tried to validate the json in jsonlint.com and it says valid Json. But gson gives parsing error.

Thanks

Brandon Mintern

unread,
Jul 25, 2012, 9:03:43 PM7/25/12
to googl...@googlegroups.com
This shouldn't be parsed as a List<Map<String, String>>, because what
you have is a JsonObject with a nested JsonArray inside that object.
One approach would be:

Message.java
===========
import com.google.gson.Gson;
import java.util.List;
import java.util.Map;

class Message {
String ip;
String game;
String zone;
List<Map<String, String>> rooms;

@Override
public String toString() {
StringBuilder roomsBuilder = new StringBuilder();
for (Map<String, String> room: rooms) {
roomsBuilder.append("\n");
for (Map.Entry<String, String> prop: room.entrySet()) {
roomsBuilder.append(" ")
.append(prop.getKey()).append(": ")
.append(prop.getValue()).append("\n");
}
}
return String.format("ip: %s\ngame: %s\nzone: %s\nrooms:\n%s",
ip, game, zone, roomsBuilder.toString());
}

public static void main(String[] args) {
Message msg = new
Gson().fromJson("{\"ip\":\"184.72.42.15\",\"game\":\"Game\",\"zone\":\"Blitz\",\"rooms\":[{\"id\":\"901\",\"left\":\"1\",\"maxplayers\":\"300\",\"players\":\"0\",\"cards_to_winners\":\"0.12\",\"cards\":\"0\",\"threshold\":\"1\",\"city\":\"New
York-744662523\"}]}", Message.class);
System.out.println(msg);
}
}
===========

$ java -cp gson-2.2.1.jar:. Message
ip: 184.72.42.15
game: Game
zone: Blitz
rooms:
id: 901
left: 1
maxplayers: 300
players: 0
cards_to_winners: 0.12
cards: 0
threshold: 1
city: New York-744662523


Another approach to building your own class is to use JsonParser to
fetch a JsonElement from the JSON. Then, you can use the
JsonObject/JsonArray API to pull the information out of the structure.

Satyarth Negi

unread,
Jul 26, 2012, 4:14:11 AM7/26/12
to googl...@googlegroups.com
You are awesome Brandon. It worked.
Actually i was not parsing rooms as <String,String> I was first parsing the main object as <String,Object> and then parsing rooms part as <String,String> and it was giving me error. But the way you described it worked even with white spaces.

Thanks alot.

Brandon Mintern

unread,
Jul 26, 2012, 3:25:41 PM7/26/12
to googl...@googlegroups.com
Ha, thanks. You're welcome.

If you like that way, using the Message class, I might suggest going
one step further and creating a Room class. That class should have the
fields you want to fetch from the rooms list, and you can give the
variables a more appropriate type. For example:

class Room {
int id;
int left;
int maxplayers;
...
float cards_to_winners;
...
String city;
}

Then, instead of having a List<Map<String, String>> rooms field in
your Message object, you would simply change it to a List<Room>
object.

Ragav G

unread,
Jul 27, 2012, 4:24:29 AM7/27/12
to googl...@googlegroups.com
How to read a two json file then merge in to new json file using GSON for android?
 

Hi,

I want to know how to read a two(A.json,B.json) json file from my android sdcard then i need to create new json file and  write in this file(c.json).

I don know how it's possible in android.

 

All ideas are welcome.

Thanks in advance....

Reply all
Reply to author
Forward
0 new messages