2012-08-08 76 views
-1

我想在java中創建一個像這樣的json。在android中創建JSON

{ 
    "contacts": [ 
     { 
      "id": "c200", 
      "name": "Ravi Tamada", 
      "email": "[email protected]", 
      "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": "[email protected]", 
      "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": "[email protected]", 
      "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": "[email protected]", 
      "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": "[email protected]", 
      "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": "[email protected]", 
      "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": "[email protected]", 
      "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": "[email protected]", 
      "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": "[email protected]", 
      "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": "[email protected]", 
      "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": "[email protected]", 
      "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": "[email protected]", 
      "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": "[email protected]", 
      "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\": \"[email protected]\", 
      \"address\": \"xx-xx-xxxx,x - street, x - country\", 
      \"gender\" : \"male\", 
      \"phone\": { 
       \"mobile\": \"+91 0000000000\", 
       \"home\": \"00 000000\", 
       \"office\": \"00 000000\" 
      } 
     } 

如何擺脫斜線。

在此先感謝

+0

拜託你們。我們應該真的回答他的問題。但是,直到他> 0%時,我們纔會用Perl回答:$ _ =〜s/\\ // g; – 2012-08-08 01:18:36

回答

0

Flexjson也是一個非常CONV如果您有POJO轉換爲JSON表示法。

JSONSerializer會序列化,JSONDeserializer會做相反的事情。

我自己用過它。

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

如果您使用的這些對象,他們應該對你去掉轉義字符的照顧。