2

I'm very new to Java. I'm just parsing a string and getting the Json Response like this:

{
  "customer_id": "user",
  "merchantId": "xxxx",
  "cards": 
  [
    {
      "card_token": "715fc10a-e7b3-48a1-b6e7-09e71ac050f8",
      "card_number": "11111111",
      "card_isin": "23232",
      "card_exp_year": "2013",
      "card_exp_month": "12"
    }
  ]
}

For some reason I wish I could be able to represent this in a POJO. Note that the cards field can consist of more than 1 field.

I'm new to Java. I don't want the code for doing it, but I want to know what is the best way to represent these structure in POJO.

Mayur Birari
  • 5,837
  • 8
  • 34
  • 61
sriram
  • 8,562
  • 19
  • 63
  • 82

4 Answers4

3

You can use GSON that can easily convert json to java object (generic) and vice versa which further you can use for your POJO.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
2

- Manual parsing of JSON to object will be a pain.

- Its better to go with Jackson, which i use.

- Or you can also choose GSON (created by google for its internal use initially).

See this link for implementation example:

http://www.mkyong.com/java/json-simple-example-read-and-write-json/

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
1

The two dominate JSON processors for Java are GSON (as @Abu points out in his answer) and Jackson. These libraries will help you map a Java POJO to a JSON object, and vice-versa.

Here is a comparison on SO.

Community
  • 1
  • 1
wulfgarpro
  • 6,666
  • 12
  • 69
  • 110
1

You can also use Jackson JSON. It's a bit faster and it works great with the Java API for RESTful Services (JAX-RS), the Java standarization for REST api's.

Here's a better comparison between the two: Jackson Vs. Gson

To read an object from String, checkout the ObjectMapper class.

Community
  • 1
  • 1
Alex
  • 2,953
  • 1
  • 27
  • 37