Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Request Urgent Help : Exception in parsing using fromJson
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
geeg  
View profile  
 More options Jul 25 2012, 5:05 pm
From: geeg <satyarth.n...@gmail.com>
Date: Wed, 25 Jul 2012 14:05:21 -0700 (PDT)
Local: Wed, Jul 25 2012 5:05 pm
Subject: Request Urgent Help : Exception in parsing using fromJson

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to ": Request Urgent Help : Exception in parsing using fromJson" by Brandon Mintern
Brandon Mintern  
View profile   Translate to Translated (View Original)
 More options Jul 25 2012, 7:42 pm
From: Brandon Mintern <mint...@easyesi.com>
Date: Wed, 25 Jul 2012 16:42:47 -0700
Local: Wed, Jul 25 2012 7:42 pm
Subject: Re: [google-gson:1745]: Request Urgent Help : Exception in parsing using fromJson
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Satyarth Negi  
View profile  
 More options Jul 25 2012, 7:55 pm
From: Satyarth Negi <satyarth.n...@gmail.com>
Date: Thu, 26 Jul 2012 01:55:27 +0200
Local: Wed, Jul 25 2012 7:55 pm
Subject: Re: [google-gson:1746]: Request Urgent Help : Exception in parsing using fromJson

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\",\"ca rds_to_winners\":\"0.12\",\"cards\":\"0\",\"threshold\":\"1\",\"city\":\"Ne w
York-744662523\"}]}";

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

Thanks

On Thu, Jul 26, 2012 at 1:42 AM, Brandon Mintern <mint...@easyesi.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brandon Mintern  
View profile  
 More options Jul 25 2012, 9:03 pm
From: Brandon Mintern <mint...@easyesi.com>
Date: Wed, 25 Jul 2012 18:03:43 -0700
Local: Wed, Jul 25 2012 9:03 pm
Subject: Re: [google-gson:1747]: Request Urgent Help : Exception in parsing using fromJson
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\":\"Blit z\",\"rooms\":[{\"id\":\"901\",\"left\":\"1\",\"maxplayers\":\"300\",\"play ers\":\"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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Satyarth Negi  
View profile  
 More options Jul 26 2012, 4:14 am
From: Satyarth Negi <satyarth.n...@gmail.com>
Date: Thu, 26 Jul 2012 10:14:11 +0200
Local: Thurs, Jul 26 2012 4:14 am
Subject: Re: [google-gson:1748]: Request Urgent Help : Exception in parsing using fromJson

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.

On Thu, Jul 26, 2012 at 3:03 AM, Brandon Mintern <mint...@easyesi.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brandon Mintern  
View profile  
 More options Jul 26 2012, 3:25 pm
From: Brandon Mintern <mint...@easyesi.com>
Date: Thu, 26 Jul 2012 12:25:41 -0700
Local: Thurs, Jul 26 2012 3:25 pm
Subject: Re: [google-gson:1749]: Request Urgent Help : Exception in parsing using fromJson
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Request Urgent Help : Exception in parsing using fromJson" by Ragav G
Ragav G  
View profile  
 More options Jul 27 2012, 4:24 am
From: Ragav G <ragav...@gmail.com>
Date: Fri, 27 Jul 2012 01:24:29 -0700 (PDT)
Local: Fri, Jul 27 2012 4:24 am
Subject: Re: Request Urgent Help : Exception in parsing using fromJson

How to read a two json file then merge in to new json file using GSON for
android?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »