2017-01-04 9 views
-3

誰かが私に良いリンクを与えたり、jsonの解析の仕組みを説明したりすることができますか?私は........ [{} {} {}]のようなオブジェクトの配列を持っています。私の値を取得しようとしています。例えば、{"name": "John" ....}私は.get(name)を呼び出してJohn.or.getString(name)という値を取得し、Johnという値を取得します。Jsonの配列とオブジェクトの解析

私は、同じオブジェクトに[{"name": "John"、 "Eta": "5"} ....という文字列があります。エラーはオブジェクトの.getstring(Eta)できません。 jsonの中には、 "Time": "(0004253200)"/

答えて

0

私はアンドロイドでJSONを操作する方法を説明しようとします。

は今 {}間の何かが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" 
       } 
     } 
    ] 
} 

のような文字列があるとします。したがって、この文字列全体を1つのJSONオブジェクトに変換できます。

です:JSONObject obj = new JSONObject(str);[]

、何をJSON配列です。上記の例では、"contacts"はJSONArrayです。

今配列JSONArray contacts = jsonObj.getJSONArray("contacts");

を取得するには、あなたが接触ID C201の名前の値を取得する必要があるとします。

JSONObject obj = new JSONObject(str); //Convert whole string to JSONObject. 
JSONArray contacts = jsonObj.getJSONArray("contacts"); //Get contacts array. 
// looping through All Contacts 
for (int i = 0; i < contacts.length(); i++) { 
    JSONObject c = contacts.getJSONObject(i); //Get JSONObject at index i 
    if(c.getString("id").equals("c201")){ 
     return c.getString("name"); 
    } 
} 

詳しくは、this articleをご覧ください。

+0

あなたの答えとリンクの間にありがとう、私はより良い理解を得ることができました。 – Vahalaru

0

のようなものがあります。JSON.stringify() - JavaScriptオブジェクトをJSON文字列に変換します。

JSON.parse() - JSON文字列をjavascriptオブジェクトに変換します。

0
String json = "{"name" :"John"}"; 
JsonObject object = new JsonObiect(json); 
String name = object.getString("name"); 
System.out.println(name); 
関連する問題