-1

I want to create a json like this in java.

{
    "contacts": [
        {
            "id": "c200",
            "name": "Ravi Tamada",
            "email": "ravi@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c201",
            "name": "Johnny Depp",
            "email": "johnny_depp@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c202",
            "name": "Leonardo Dicaprio",
            "email": "leonardo_dicaprio@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c203",
            "name": "John Wayne",
            "email": "john_wayne@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c204",
            "name": "Angelina Jolie",
            "email": "angelina_jolie@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "female",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c205",
            "name": "Dido",
            "email": "dido@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "female",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c206",
            "name": "Adele",
            "email": "adele@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "female",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c207",
            "name": "Hugh Jackman",
            "email": "hugh_jackman@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c208",
            "name": "Will Smith",
            "email": "will_smith@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c209",
            "name": "Clint Eastwood",
            "email": "clint_eastwood@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c2010",
            "name": "Barack Obama",
            "email": "barack_obama@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c2011",
            "name": "Kate Winslet",
            "email": "kate_winslet@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "female",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c2012",
            "name": "Eminem",
            "email": "eminem@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    }
]
}

I am using the library org.json.

But when i convert the json into string there comes a lot of slashes.

Like this:

 {
            \"id\": \"c200\",
            \"name\": \"Ravi Tamada\",
            \"email\": \"ravi@gmail.com\",
            \"address\": \"xx-xx-xxxx,x - street, x - country\",
            \"gender\" : \"male\",
            \"phone\": {
                \"mobile\": \"+91 0000000000\",
                \"home\": \"00 000000\",
                \"office\": \"00 000000\"
            }
        }

How to get rid of the slashes.

thanks in advance

Pramod J George
  • 1,725
  • 12
  • 24
  • C'mon you guys. We should really answer his question. But until he's >0%, we'll answer in Perl: $_ =~ s/\\//g; – Yusuf X Aug 08 '12 at 01:18

2 Answers2

0

As I recall, the \ character is escaping the ". It's supposed to be there. How do you want to use the JSON data? It would be nice if you could provide sample code for what you are doing with it. In my application I do the following,

BufferedReader reader = new BufferedReader(new 
    InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
JSONTokener tokener = new JSONTokener(json);
JSONObject JSONParams = new JSONObject(tokener);
String param1 = eventParams.getString("param1");
...

If you use these objects they should take care of removing the escape characters for you.

John Sallay
  • 219
  • 3
  • 8
0

Flexjson is also a very convenient way if you are have POJOs to be converted to JSON representations.

JSONSerializer will serialize and JSONDeserializer will do the reverse.

I have used it by myself.

Link to Flexjson Website - http://flexjson.sourceforge.net/

public String doSomething( Object arg1, ... ) {

    Person p = ...load a person...;

    JSONSerializer serializer = new JSONSerializer();
    return serializer.serialize( p );
}

{
    "class": "Person",
    "name": "William Shakespeare",
    "birthday": -12802392000000,
    "nickname": "Bill"
}
prageeth
  • 751
  • 8
  • 23