-1

このようなjsonを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"
            }
    }
]
}

ライブラリorg.jsonを使用しています。

しかし、json を文字列に変換すると、多くのスラッシュが発生します。

このような:

 {
            \"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\"
            }
        }

スラッシュを取り除く方法。

前もって感謝します

4

2 に答える 2

0

私が思い出したように、\ 文字は " をエスケープしています。それはそこにあるはずです。JSON データをどのように使用しますか?それを使用して何をしているかのサンプル コードを提供していただければ幸いです。私のアプリケーションでは、私は以下をせよ、

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");
...

これらのオブジェクトを使用する場合、エスケープ文字を削除する必要があります。

于 2012-08-08T01:18:15.427 に答える
0

Flexjsonは、 JSON表現に変換する POJO がある場合にも非常に便利な方法です。

JSONSerializer はシリアライズし、JSONDeserializer はその逆を行います。

私は自分でそれを使用しました。

Flexjson Web サイトへのリンク - 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"
}
于 2012-08-08T01:27:50.663 に答える