访问Javascript对象

时间:2014-01-21 09:40:38

标签: javascript jquery cordova

可能是一个简单的问题,但在一些搜索之后似乎无法解决它:) ..场景如下:

使用PhoneGap,使用jquery通过Ajax接收json对象。该对象需要显示在下一个屏幕上(首先是搜索页面,下一个是搜索结果页面)。

当收到对象时,它将保存在sessionStorage变量中(例如sessionStorage.result = data)。

但是当试图在下一页上访问它时,它会给出一个错误,说明该属性是未知的。 e.g。

var result = sessionStorage.result;

alert(result.response.businesses[0].name);

还尝试过:

alert($(result.response.businesses[0].name));

它说这个属性是未知的。 json的基本结构如下:

{
  "action": "SearchBusiness",
  "meta": {
    "code": 200,
    "message": "OK"
  },
  "response": {
    "searchQuery": {
      "categoryId": 4,
      "communityId": 4,
      "latitude": "",
      "longitude": "",
      "category": "Grocery Stores",
      "community": "Indian/Pakistani",
      "searchRadius": "",
      "city": "",
      "postalCode": ""
    },
    "businesses": [
      {
"businessId": "2",
        "name": "Name",
        "address": "123",
        "phone": "(123) 456 7890",
        "city": "any city",
        "country": "United States",
        "state": "Abc",
        "postalCode": "a123",
        "url": "",
        "logoUrl": null,
        "latitude": "0.1951704",
        "longitude": "-1.89512",
        "categoryId": "4",
        "communityId": "4",
        "ratings": "0",
        "ratingAvg": "0.00",
        "distance": 0
      }
]

任何帮助表示感谢。

3 个答案:

答案 0 :(得分:0)

您错误地访问了该值,

尝试,

var result = JSON.parse(sessionStorage.result);    
alert($(result.businesses[0].name));

答案 1 :(得分:0)

在您的json代码中丢失}您的代码必须像那样

{
    "action": "SearchBusiness",
    "meta": {
        "code": 200,
        "message": "OK"
    },
    "response": {
        "searchQuery": {
            "categoryId": 4,
            "communityId": 4,
            "latitude": "",
            "longitude": "",
            "category": "Grocery Stores",
            "community": "Indian/Pakistani",
            "searchRadius": "",
            "city": "",
            "postalCode": ""
        },
        "businesses": [{
            "businessId": "2",
            "name": "Name",
            "address": "123",
            "phone": "(123) 456 7890",
            "city": "any city",
            "country": "United States",
            "state": "Abc",
            "postalCode": "a123",
            "url": "",
            "logoUrl": null,
            "latitude": "0.1951704",
            "longitude": "-1.89512",
            "categoryId": "4",
            "communityId": "4",
            "ratings": "0",
            "ratingAvg": "0.00",
            "distance": 0
        }]
    }
}

警报(result.response.businesses [0]。名称); //我认为它会起作用。没有错误 还看看sessiontroge有没有数据

答案 2 :(得分:0)

您的Json字符串不是有效的JSON格式,请检查格式

{
"action": "SearchBusiness",
"meta": {
    "code": 200,
    "message": "OK"
},
"response": {
    "searchQuery": {
        "categoryId": 4,
        "communityId": 4,
        "latitude": "",
        "longitude": "",
        "category": "Grocery Stores",
        "community": "Indian/Pakistani",
        "searchRadius": "",
        "city": "",
        "postalCode": ""
    },
    "businesses": [{
        "businessId": "2",
        "name": "Name",
        "address": "123",
        "phone": "(123) 456 7890",
        "city": "any city",
        "country": "United States",
        "state": "Abc",
        "postalCode": "a123",
        "url": "",
        "logoUrl": null,
        "latitude": "0.1951704",
        "longitude": "-1.89512",
        "categoryId": "4",
        "communityId": "4",
        "ratings": "0",
        "ratingAvg": "0.00",
        "distance": 0
    }]
}

}