私はアンドロイドで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をご覧ください。
あなたの答えとリンクの間にありがとう、私はより良い理解を得ることができました。 – Vahalaru