3

I was able to successfully parse the below JSON string in Android using JSONObject and JSONArray. Have had no success achieving the same result with GSON or Jackson. Can someone help me with code fragments including POJO definitions to parse this with GSON and Jackson?

{
    "response":{
        "status":200
    },
    "items":[
        {
            "item":{
                "body":"Computing"
                "subject":"Math"
                "attachment":false,
        }
    },
    {
        "item":{
           "body":"Analytics"
           "subject":"Quant"
           "attachment":true,
        }
    },

],
"score":10,
 "thesis":{
        "submitted":false,
        "title":"Masters"
        "field":"Sciences",        
    }
}
Dee Hydration
  • 31
  • 1
  • 3
  • 1
    Maybe you could include POJO definitions you did try, to give an idea of what might have gone wrong? Basic idea is just to match the structures. – StaxMan Oct 31 '11 at 17:58
  • Also, when posting a question, I recommend putting in the effort to ensure any code or JSON examples are valid and correct. The JSON example in the original question is invalid, and leaves folks that might help or learn from this thread guessing about what's what. http://jsonlint.com can be used to quickly and easily validate JSON. – Programmer Bruce Nov 11 '11 at 19:29

1 Answers1

8

Following are simple examples of using Gson and Jackson to deserialize/serialize JSON (similar to the invalid JSON in the original question) to/from a matching Java data structure.

The JSON:

{
    "response": {
        "status": 200
    },
    "items": [
        {
            "item": {
                "body": "Computing",
                "subject": "Math",
                "attachment": false
            }
        },
        {
            "item": {
                "body": "Analytics",
                "subject": "Quant",
                "attachment": true
            }
        }
    ],
    "score": 10,
    "thesis": {
        "submitted": false,
        "title": "Masters",
        "field": "Sciences"
    }
}

The Matching Java Data Structure:

class Thing
{
  Response response;
  ItemWrapper[] items;
  int score;
  Thesis thesis;
}

class Response
{
  int status;
}

class ItemWrapper
{
  Item item;
}

class Item
{
  String body;
  String subject;
  boolean attachment;
}

class Thesis
{
  boolean submitted;
  String title;
  String field;
}

Jackson Example:

import java.io.File;

import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.map.ObjectMapper;

public class JacksonFoo
{
  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper();  
    mapper.setVisibilityChecker(  
      mapper.getVisibilityChecker()  
        .withFieldVisibility(Visibility.ANY));
    Thing thing = mapper.readValue(new File("input.json"), Thing.class);
    System.out.println(mapper.writeValueAsString(thing));
  }
}

Gson Example:

import java.io.FileReader;

import com.google.gson.Gson;

public class GsonFoo
{
  public static void main(String[] args) throws Exception
  {
    Gson gson = new Gson();
    Thing thing = gson.fromJson(new FileReader("input.json"), Thing.class);
    System.out.println(gson.toJson(thing));
  }
}
Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97