Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By jim829

Thread: how to generate this json ?

  1. #1
    mlotfi is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default how to generate this json ?

    Hi,

    I need to generate this json :

    XML Code:
    "agencies": {
            "1": {
                assignedAgencies: [
                    "agency1",
                    "agency2",
                    "agency5"],
                restOfAgencies: [
                    "agency3",
                    "agency4",
                    "agency6"],
                global: "true"
            },
                "2": {
                assignedAgencies: [
                    "agency6",
                    "agency5"],
                restOfAgencies: [
                    "agency1",
                    "agency2",
                    "agency3",
                    "agency4"],
                 global: "false"
            }
    
        }

    I have two java list :
    assignedAgenciesList
    restOfAgenciesList

    and a string :
    global (string )

    I would like to create a function :
    generateJson (List assignedAgenciesList, List restOfAgenciesList,String global) { ...} to genare the above json.

    Thanks, your help is appreciated.
    Last edited by mlotfi; 02-06-2015 at 08:25 PM.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    384
    Rep Power
    11

    Default Re: how to generate this json ?

    I would take a look at GSON, it is a good library to serialize and deserialize Java objects to and from JSON, or, in your case, you can use it to get a toJSON representation.

    What do your classes look like? Do you have a class that represents an Agency?

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: how to generate this json ?

    Sorry, but we don't write code for forum members here. But since you joined this forum over six years ago you should be able to make a good attempt, right?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: how to generate this json ?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: how to generate this json ?

    So strange - the exact same question gets the exact same answer.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: how to generate this json ?

    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: how to generate this json ?

    The OP could also wait for Java 9.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: how to generate this json ?

    Quote Originally Posted by jim829 View Post
    The OP could also wait for Java 9.

    Regards,
    Jim
    Is Java 9 going to write the code for you?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: how to generate this json ?

    Quote Originally Posted by gimbal2 View Post
    Is Java 9 going to write the code for you?
    Yep! You just create an instance of WriteThisCode and then invoke, doItNow().

    Regards,
    Jim
    DarrylBurke likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. How to get XML attributes in Java and generate JSON?
    By blertadhimitri in forum New To Java
    Replies: 0
    Last Post: 12-02-2013, 04:21 PM
  2. Ask Json
    By ultra.coder in forum Advanced Java
    Replies: 0
    Last Post: 11-19-2013, 04:08 AM
  3. Json
    By mac in forum New To Java
    Replies: 10
    Last Post: 06-08-2010, 11:15 PM
  4. JSON Lib: json-lib-1.0-jdk13.jar
    By Raghuraman K in forum Advanced Java
    Replies: 3
    Last Post: 05-13-2010, 10:00 AM
  5. JSON Parsing
    By prakashkadakol in forum New To Java
    Replies: 1
    Last Post: 01-04-2010, 07:13 PM

Tags for this Thread

Posting Permissions

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