Results 1 to 2 of 2
  1. #1
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    48
    Rep Power
    0

    Default Write POJO for JSON string

    Hi All,

    I have a JSON string
    Java Code:
    {
      "AvailableDeliveries": {
        "500": {
          "canOverride": true,
          "code": "INOFFICE",
          "critTypeId": null,
          "desc": "In Office",
          "id": 500,
          "name": "IN OFFICE"
        },
        "502": {
          "canOverride": true,
          "code": "TELEPHONE",
          "critTypeId": 5146,
          "desc": "Telephone",
          "id": 502,
          "name": "TELEPHONE"
        },
        "503": {
          "canOverride": true,
          "code": "DIY DOC ACCESS",
          "critTypeId": 5469,
          "desc": "Online Documents",
          "id": 503,
          "name": "DIY DOC ACCESS"
        }
      }
    }
    Number of JSON object can vary. I tried to generate POJO from jsonschema2pojo but it generate fix number of object inside AvailableDeliveries class. How I can define a POJO with collection of object and map corresponding field for above JSON string. I can't change the structure of JSON response as I am consuming the string.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Write POJO for JSON string

    The first time I ran into this requirement I attacked it by turning the problem around: I would have a simplistic POJO and then try to emit that as JSON (I used Google GSON at the time which is really easy to use). And then make changes to the POJO and see how that would affect the JSON data that would come out of it.

    In the end I had a setup where I would emit the same JSON that I would need to consume and as if by magic, it worked the other way around too.

    Usually I am for reading the manual, but in this particular case dumb experimentation is equally reliable and teaches a lot. In your case you have an easy start: you can already see from the data that you need a pojo which holds 6 properties.

    Delivery
    - canOverride: boolean
    - code: String
    - critTypeId: Integer
    - desc: String
    - id: Integer
    - name: String

    Start with that. Try to emit and read JSON data which is a single JSON object not a map. Then try to read a Map of them.

    https://code.google.com/p/google-gson/
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 2
    Last Post: 08-26-2013, 12:32 AM
  2. how to write the String Array into the own file
    By nirasiva in forum New To Java
    Replies: 2
    Last Post: 06-16-2011, 08:13 AM
  3. Replies: 8
    Last Post: 11-12-2010, 04:00 PM
  4. JSON Lib: json-lib-1.0-jdk13.jar
    By Raghuraman K in forum Advanced Java
    Replies: 3
    Last Post: 05-13-2010, 11:00 AM
  5. How to write multiline String in a JLabel
    By JavaBean in forum AWT / Swing
    Replies: 4
    Last Post: 12-14-2009, 06:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •