如何使用JavaScript从JSON对象获取数据

时间:2015-03-03 07:13:58

标签: javascript json

{   
 "action" : "get",
 "application" : "2c5ca3b0-be74-11e4-8ff3-f7af49a474ef",
 "params" : {
 "ql" : [ "select * where username='pqr'" ]
},
 "path" : "/logs",
 "uri" : "https://api.usergrid.com/serv-d/demo1/logs",
 "entities" : [ {
 "uuid" : "97b0fd0a-be74-11e4-9324-b3bd8af7859e",
 "type" : "log",
 "created" : 1425036869840,
 "modified" : 1425036869840,
 "metadata" : {
  "path" : "/logs/97b0fd0a-be74-11e4-9324-b3bd8af7859e"
 },
 "password" : "pqr",
 "username" : "pqr"
 }],
 "timestamp" : 1425359738746,
 "duration" : 15,
 "organization" : "serv-d",
 "applicationName" : "demo1",
"count" : 1
  }

这是我的应用程序的服务器端响应,我只想获取用户名和密码值。

6 个答案:

答案 0 :(得分:0)

获取变量中的数据,比如变量数据。

var data = {   
     "action" : "get",
     "application" : "2c5ca3b0-be74-11e4-8ff3-f7af49a474ef",
     "params" : {
     "ql" : [ "select * where username='pqr'" ]
    },
     "path" : "/logs",
     "uri" : "https://api.usergrid.com/serv-d/demo1/logs",
     "entities" : [ {
     "uuid" : "97b0fd0a-be74-11e4-9324-b3bd8af7859e",
     "type" : "log",
     "created" : 1425036869840,
     "modified" : 1425036869840,
     "metadata" : {
      "path" : "/logs/97b0fd0a-be74-11e4-9324-b3bd8af7859e"
     },
     "password" : "pqr",
     "username" : "pqr"
     }],
     "timestamp" : 1425359738746,
     "duration" : 15,
     "organization" : "serv-d",
     "applicationName" : "demo1",
    "count" : 1
};
//logging username and password fields
console.log(data.entities[0].username, data.entities[0].password);

答案 1 :(得分:0)

JSON的行为类似于键值对类的集合。将此Json分配给Javascript变量,然后您可以使用相应的键(如

)获取JSON的值
     var myJson = 
      {
         "action" : "get", "application" : "2c5ca3b0-be74-11e4-8ff3-
          f7af49a474ef", "params" : { "ql" : [ "select * where   
           username='pqr'" ] }, "path" : "/logs", "uri" : 
          "https://api.usergrid.com/serv-d/demo1/logs", "entities" : [ 
          { "uuid" : "97b0fd0a-be74-11e4-9324-b3bd8af7859e", "type" : 
           "log", "created" : 1425036869840, "modified" :  
             1425036869840, "metadata" : 
             { "path" : "/logs/97b0fd0a-be74-11e4-9324-b3bd8af7859e"  
              }, "pas           sword" : "pqr", "username" : "pqr" 
             }],  "timestamp" : 1425359738746, "duration" : 15, 
               "organization" : "serv-d", "applicationName" : "demo1",
                  "count" : 1 };


            var action = myJSON.action;
             var uri = myJSON.uri; and so on..

答案 2 :(得分:0)

假设您将其存储为“obj”,那么您可以obj.entities[0].username(同样也可以输入密码。

答案 3 :(得分:0)

假设您将其称为data,您可以使用data.entities[0].username来访问用户名。

var data = {
    "action": "get",
    "application": "2c5ca3b0-be74-11e4-8ff3-f7af49a474ef",
    "params": {
        "ql": [
            "select * where username='pqr'"
        ]
    },
    "path": "/logs",
    "uri": "https://api.usergrid.com/serv-d/demo1/logs",
    "entities": [
        {
            "uuid": "97b0fd0a-be74-11e4-9324-b3bd8af7859e",
            "type": "log",
            "created": 1425036869840,
            "modified": 1425036869840,
            "metadata": {
                "path": "/logs/97b0fd0a-be74-11e4-9324-b3bd8af7859e"
            },
            "password": "pqr",
            "username": "pqr"
        }
    ],
    "timestamp": 1425359738746,
    "duration": 15,
    "organization": "serv-d",
    "applicationName": "demo1",
    "count": 1
};
alert(data.entities[0].username)
alert(data.entities[0].password)

答案 4 :(得分:0)

使用JSON.parse()将JSON响应解析为Javascript对象。然后,您可以访问属性。

var obj = JSON.parse(response);

用户名和密码位于entities属性中的数组中,因此您可以按以下方式访问它们:

var username = obj.entities[0].username;
var password = obj.entities[0].password;

答案 5 :(得分:0)

 var yourData = {   
 "action" : "get",
 "application" : "2c5ca3b0-be74-11e4-8ff3-f7af49a474ef",
 "params" : {
 "ql" : [ "select * where username='pqr'" ]
},
 "path" : "/logs",
 "uri" : "https://api.usergrid.com/serv-d/demo1/logs",
 "entities" : [ {
 "uuid" : "97b0fd0a-be74-11e4-9324-b3bd8af7859e",
 "type" : "log",
 "created" : 1425036869840,
 "modified" : 1425036869840,
 "metadata" : {
  "path" : "/logs/97b0fd0a-be74-11e4-9324-b3bd8af7859e"
 },
 "password" : "pqr",
 "username" : "pqr"
 }],
 "timestamp" : 1425359738746,
 "duration" : 15,
 "organization" : "serv-d",
 "applicationName" : "demo1",
"count" : 1
}

因为用户名和密码是实体的数据 - 是一个元素成员的数组 您可以访问用户名数据

var username = yourData.entities[0].username

同样适用于密码