Search code examples
javajsonparsinggsonorg.json

GSON: MalformedJsonException when running the jar file (works fine in Eclipse)


This is my first post here, hope someone can help me on this because I can't understand what's wrong.

I have a java method that parses a JSON String

    public static String getFieldFrom(String field, String event) {
    try {
        JsonElement jelement = new JsonParser().parse(event);
        JsonObject  obj = jelement.getAsJsonObject();
        return obj.getAsJsonObject("from").get(field).getAsString();            
    }catch(Exception e) {
        System.out.println("Error parsing field " + field + ": " + e);
    }       
    return "-1";
}

Where the event is the string, and field the field I'm interested in. The program works fine when running on eclipse. If I compile it as a jar and I try to run it I get an Exception:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 3 path $

The JSON string that's being processed is the same, this one (looks valid to me):

{"event": "message", "id": "00000000b0a2a54e1a01000000000000d", "flags": 258, "fwd_from": {"id": "$01000000428d8006442ef91f3ed48e27", "peer_type": "user", "peer_id": 109088066, "print_name": "Sample_Name", "flags": 1, "first_name": "Name", "last_name": "Sample", "username": "sampleuser"}, "fwd_date": 1522850949, "from": {"id": "$050023200b0a234e82865474b11fd9cd", "peer_type": "channel", "peer_id": 123232323, "print_name": "ChnNameTest", "flags": 19609, "title": "ChnNameTest", "participants_count": 0, "admins_count": 0, "kicked_count": 0}, "to": {"id": "$011200000b0a2a54e812345674b47fd9cd", "peer_type": "channel", "peer_id": 1319412121236, "print_name": "ChnNameTest", "flags": 196609, "title": "ChnNameTest", "participants_count": 0, "admins_count": 0, "kicked_count": 0}, "out": true, "unread": false, "service": false, "date": 1522850949, "text": "This is the message text"}

I'm trying to get the "id" field, under "from" object. I've also tried with another library (org.json) but the behavior is the same. Again, if I run the code on eclipse it's working. I really don't know what to try... Hope someone can help me! Thank you!

UPDATE: The problem seems to be related with the string passed. So this is the piece of code that reads the stdout of a script i'm calling (that sends back json text).

    Process child = Runtime.getRuntime().exec(command);
    InputStream in = child.getInputStream();
    int c;
    char ca;
    String line="";
    while ((c = in.read()) != -1) { //Read stdout char by char
          ca=(char)c;
          if(ca=='\n' || ca=='\r') { //Got a line
            if(line.contains("{\"event\":")) {  
                System.out.println(getFieldFrom("id",line)));
            }                   
            line="";
          }else {
            line=line+ca;
          }
      }
      in.close();

Solution

  • This is working for me.

            String json="{\"event\": \"message\", \"id\": \"00000000b0a2a54e1a01000000000000d\", \"flags\": 258, \"fwd_from\": {\"id\": \"$01000000428d8006442ef91f3ed48e27\", \"peer_type\": \"user\", \"peer_id\": 109088066, \"print_name\": \"Sample_Name\", \"flags\": 1, \"first_name\": \"Name\", \"last_name\": \"Sample\", \"username\": \"sampleuser\"}, \"fwd_date\": 1522850949, \"from\": {\"id\": \"$050023200b0a234e82865474b11fd9cd\", \"peer_type\": \"channel\", \"peer_id\": 123232323, \"print_name\": \"ChnNameTest\", \"flags\": 19609, \"title\": \"ChnNameTest\", \"participants_count\": 0, \"admins_count\": 0, \"kicked_count\": 0}, \"to\": {\"id\": \"$011200000b0a2a54e812345674b47fd9cd\", \"peer_type\": \"channel\", \"peer_id\": 1319412121236, \"print_name\": \"ChnNameTest\", \"flags\": 196609, \"title\": \"ChnNameTest\", \"participants_count\": 0, \"admins_count\": 0, \"kicked_count\": 0}, \"out\": true, \"unread\": false, \"service\": false, \"date\": 1522850949, \"text\": \"This is the message text\"}";
        Gson gson = new Gson();
        JsonObject obj = gson.fromJson(json, JsonElement.class).getAsJsonObject();
        String str = obj.getAsJsonObject("from").get("id").getAsString();
        System.out.println(str);
    

    Output:

    $050023200b0a234e82865474b11fd9cd