Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 解析Json数组和对象_Android_Arrays_Json - Fatal编程技术网

Android 解析Json数组和对象

Android 解析Json数组和对象,android,arrays,json,Android,Arrays,Json,有人能给我一个好的链接或帮助解释解析json的工作原理吗。我有一个像这样的物体阵列。。。。。。。。[{}{}{}]. 我正在尝试获取例如{“name”:“John”…}的值。我调用.get(name)获取值John。或者.getString(name)获取值John 我遇到的另一件事是在同一个对象上有[{“name”:“John”,“Eta”:“5”}….]我试图调用getstring(Eta),但在对象上出现了一个错误can't.getstring(Eta)。这可能与某些json具有类似于/“

有人能给我一个好的链接或帮助解释解析json的工作原理吗。我有一个像这样的物体阵列。。。。。。。。[{}{}{}]. 我正在尝试获取例如{“name”:“John”…}的值。我调用.get(name)获取值John。或者.getString(name)获取值John

我遇到的另一件事是在同一个对象上有[{“name”:“John”,“Eta”:“5”}….]我试图调用getstring(Eta),但在对象上出现了一个错误can't.getstring(Eta)。这可能与某些json具有类似于/“Time”的内容有关:“(0004253200)”/

json.stringify()将javascript对象转换为json字符串


parse()-将JSON字符串转换为javascript对象。

我将尝试解释如何在android中使用JSON

String json = "{"name" :"John"}";
JsonObject object = new JsonObiect(json);
String name = object.getString("name");
System.out.println(name);
假设你有一根像

{
    "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"
                }
        }
  ]
}
现在,
{}
之间的任何内容都是JSON对象。因此,整个字符串可以转换为一个JSON对象

为此:
jsonobjectobj=newjsonobject(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");
    }
}
JSONObject obj=新的JSONObject(str)//将整个字符串转换为JSONObject。
JSONArray contacts=jsonObj.getJSONArray(“contacts”)//获取联系人数组。
//通过所有触点循环
对于(int i=0;i

请查看更多阅读材料。

谢谢您的回答和链接,我对此有了更好的理解。