无法访问json对象中的数据

时间:2015-03-04 09:20:05

标签: javascript jquery json alert

我是这个平台的新手。如果有人能帮助我,那真的很棒。

- - - - > 我有一个变量globalsyncedinData,其中包含json文本格式的以下数据。

{
    "rows": [
        {
            "key": "1234",
            "id": "1234",
            "value": {
                "rev": "1-efaa6f87b907297b8faa3423c1abbe4b"
            },
            "doc": {
                "_id": "1234",
                "_rev": "1-efaa6f87b907297b8faa3423c1abbe4b",
                "acresowned": "2000",
                "address": "6543288",
                "age": "45",
                "channels": "JAVATP",
                "email": "Windows @gmail. com",
                "gender": "Male",
                "homephone": "37884321155",
                "imageURL": "http://119.226.229.61:8080/abcd/media/0.28967773471958935IMAGE_FAR.jpg",
                "mobile": "58899110",
                "pincode": "334455",
                "qualification": "be",
                "regularcrops": "",
                "seasonalcrops": "",
                "seedbrands": "wheat",
                "soiltype": "gtred",
                "sourceofwater": "nadi",
                "state": "maharashtra",
                "username": "windows"
            }
        },
        {
            "key": "123455",
            "id": "123455",
            "value": {
                "rev": "1-bf67fc273b8e64723895d2caf7fb90d1"
            },
            "doc": {
                "_id": "123455",
                "_rev": "1-bf67fc273b8e64723895d2caf7fb90d1",
                "acresowned": "",
                "address": "",
                "age": "45",
                "channels": "JAVATP",
                "email": "win7@gmail.com",
                "gender": "Male",
                "homephone": "",
                "imageURL": "http://119.226.229.61:8080/abcd/media/0.2573327284771949IMAGE_FAR.jpg",
                "mobile": "6326678990",
                "pincode": "",
                "qualification": "be",
                "regularcrops": "",
                "seasonalcrops": "",
                "seedbrands": "",
                "soiltype": "",
                "sourceofwater": "",
                "state": "",
                "username": "win7"
            }
        },
        {
            "key": "1424951086535",
            "id": "1424951086535",
            "value": {
                "rev": "1-6f7d2dca1cc0045ec4f2d7d2aadfc31c"
            },
            "doc": {
                "_id": "1424951086535",
                "_rev": "1-6f7d2dca1cc0045ec4f2d7d2aadfc31c",
                "acresowned": "6",
                "address": "addr",
                "age": "56",
                "channels": "JAVATP",
                "email": "email@go.com",
                "gender": "Male",
                "homephone": "9999999999",
                "imageURL": "",
                "mobile": "1111111111",
                "pincode": "751059",
                "qualification": "ed qual",
                "regularcrops": "reg",
                "seasonalcrops": "seasonal",
                "seedbrands": "seed",
                "soiltype": "soil",
                "sourceofwater": "water",
                "state": "state",
                "username": "amitabh"
            }
        }
    ],
    "total_rows": 4,
    "update_seq": 10
}

我的代码段:

function setsyncUserDetails(tx) {

     alert("globalsyncedinData: "+window.globalsyncedinData);
     alert('into setsyncUserDetails method');
     var obj = JSON.parse(window.globalsyncedinData);
     alert('parsed string into json object is:'+obj);
     alert("First key is:"+ obj.rows[0].key);
     ...
}

执行时,我收到警告框,直到“解析为json对象的字符串为[object Object]”。它停在那里。我无法访问键值,我没有得到它后面的警告框,即警报(“第一个键是:”+ obj.rows [0] .key);没有开放。

我在Android平台上开发应用程序,所以我需要警报框。我实际上想要一个可以访问globalsyncedinData中的“行”的变量,这样我就可以使用$ .each()迭代每一行。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

在这里检查工作小提琴。 http://jsfiddle.net/eqrmmmkz/

var text = '{"rows":[ {"key":"1234","id":"1234","value":{"rev":"1-efaa6f87b907297b8faa3423c1abbe4b"},"doc":{"_id":"1234","_rev":"1-efaa6f87b907297b8faa3423c1abbe4b","acresowned":"2000","address":"6543288","age":"45","channels":"JAVATP","email":"Windows @gmail. com","gender":"Male","homephone":"37884321155","imageURL":"http://119.226.229.61:8080/abcd/media/0.28967773471958935IMAGE_FAR.jpg","mobile":"58899110","pincode":"334455","qualification":"be","regularcrops":"","seasonalcrops":"","seedbrands":"wheat","soiltype":"gtred","sourceofwater":"nadi","state":"maharashtra","username":"windows"}} ,{"key":"123455","id":"123455","value":{"rev":"1-bf67fc273b8e64723895d2caf7fb90d1"},"doc":{"_id":"123455","_rev":"1-bf67fc273b8e64723895d2caf7fb90d1","acresowned":"","address":"","age":"45","channels":"JAVATP","email":"win7@gmail.com","gender":"Male","homephone":"","imageURL":"http://119.226.229.61:8080/abcd/media/0.2573327284771949IMAGE_FAR.jpg","mobile":"6326678990","pincode":"","qualification":"be","regularcrops":"","seasonalcrops":"","seedbrands":"","soiltype":"","sourceofwater":"","state":"","username":"win7"}} ,{"key":"1424951086535","id":"1424951086535","value":{"rev":"1-6f7d2dca1cc0045ec4f2d7d2aadfc31c"},"doc":{"_id":"1424951086535","_rev":"1-6f7d2dca1cc0045ec4f2d7d2aadfc31c","acresowned":"6","address":"addr","age":"56","channels":"JAVATP","email":"email@go.com","gender":"Male","homephone":"9999999999","imageURL":"","mobile":"1111111111","pincode":"751059","qualification":"ed qual","regularcrops":"reg","seasonalcrops":"seasonal","seedbrands":"seed","soiltype":"soil","sourceofwater":"water","state":"state","username":"amitabh"}} ], "total_rows":4,"update_seq":10}';  
obj = JSON.parse(text);  
document.getElementById("demo").innerHTML = obj.rows[0].key;    

更新:

http://jsfiddle.net/eqrmmmkz/1/