访问JSON数据时出现未定义的错误

时间:2016-06-20 08:51:11

标签: javascript json

 $http({
     method: 'GET',
    // url: "https://na30.salesforce.com/services/data/v36.0/query?q=SELECT+id,+name,+Product2.Name,+Product2.Description,+Product2Id,+Pricebook2Id,+Product2.Family,+Product2.Product_Image_URL__c,+Product2.Product_Image__c,+Pricebook2.name+from+PricebookEntry+where+(Pricebook2.Id+=+'01s360000049JQh'+and+Product2.Family+=+'CURRIES')",
     url:  $scope.finalurl,
     headers: {  'Content-Type': 'application/json' ,
                  'Authorization' : $scope.result1 }
   }).success(function(response) {
    $scope.result= response.totalSize;
 //   $scope.size = response.
 //   alert(" Products records captured");
 //  alert($scope.result);
    var y =response.records;
    alert(y);
    localStorage.setItem("offlinecurries1",JSON.stringify(y));
    var x =localStorage.getItem("offlinecurries1");
    alert(JSON.stringify(x[0].Name));

在上述警告

中收到未定义的错误

我的回复数据是

{
    "totalSize": 497,
    "done": true,
    "records": [{
        "attributes": {
            "type": "PricebookEntry",
            "url": "/services/data/v36.0/sobjects/PricebookEntry/01u36000001de83AAA"
        },
        "Id": "01u36000001de83AAA",
        "Name": "P-0025",
        "Pricebook2Id": "01s36000004AGVLAA4",
        "Product2": {
            "attributes": {
                "type": "Product2",
                "url": "/services/data/v36.0/sobjects/Product2/01t36000001To1lAAC"
            },
            "Product_Image_URL__c": null,
            "Description": "ACHAPPAM175GMS X12PKT",
            "Family": null,
            "Product_Image__c": "<img src=\" \" alt=\"Not Avaialble\" height=\"50\" width=\"50\" border=\"0\"/>"
        },
        "Product2Id": "01t36000001To1lAAC",
        "Pricebook2": {
            "attributes": {
                "type": "Pricebook2",
                "url": "/services/data/v36.0/sobjects/Pricebook2/01s36000004AGVLAA4"
            },
            "Name": "UK Pricebook"
        }
    }, {
        "attributes": {
            "type": "PricebookEntry",
            "url": "/services/data/v36.0/sobjects/PricebookEntry/01u36000001de84AAA"
        },
        "Id": "01u36000001de84AAA",
        "Name": "P-0026",
        "Pricebook2Id": "01s36000004AGVLAA4",
        "Product2": {
            "attributes": {
                "type": "Product2",
                "url": "/services/data/v36.0/sobjects/Product2/01t36000001To1mAAC"
            },
            "Product_Image_URL__c": null,
            "Description": "ADA RICE 200 x 30",
            "Family": null,
            "Product_Image__c": "<img src=\" \" alt=\"Not Avaialble\" height=\"50\" width=\"50\" border=\"0\"/>"
        },
        "Product2Id": "01t36000001To1mAAC",
        "Pricebook2": {
            "attributes": {
                "type": "Pricebook2",
                "url": "/services/data/v36.0/sobjects/Pricebook2/01s36000004AGVLAA4"
            },
            "Name": "UK Pricebook"
        }
    }]
}

2 个答案:

答案 0 :(得分:0)

var x =localStorage.getItem("offlinecurries1");

此行返回一个字符串!

您需要将字符串解析为json对象,在

之后添加此行
var x =localStorage.getItem("offlinecurries1");
x = JSON.parse(x)

答案 1 :(得分:0)

试试这个。保存时需要strigify,在检索时需要使用parse JSON.parse json,以便将其转换回数组。

var json = { "totalSize": 497, "done": true, "records": [{ "attributes": { "type": "PricebookEntry", "url": "/services/data/v36.0/sobjects/PricebookEntry/01u36000001de83AAA" }, "Id": "01u36000001de83AAA", "Name": "P-0025", "Pricebook2Id": "01s36000004AGVLAA4", "Product2": { "attributes": { "type": "Product2", "url": "/services/data/v36.0/sobjects/Product2/01t36000001To1lAAC" }, "Product_Image_URL__c": null, "Description": "ACHAPPAM175GMS X12PKT", "Family": null, "Product_Image__c": "" }, "Product2Id": "01t36000001To1lAAC", "Pricebook2": { "attributes": { "type": "Pricebook2", "url": "/services/data/v36.0/sobjects/Pricebook2/01s36000004AGVLAA4" }, "Name": "UK Pricebook" } }, { "attributes": { "type": "PricebookEntry", "url": "/services/data/v36.0/sobjects/PricebookEntry/01u36000001de84AAA" }, "Id": "01u36000001de84AAA", "Name": "P-0026", "Pricebook2Id": "01s36000004AGVLAA4", "Product2": { "attributes": { "type": "Product2", "url": "/services/data/v36.0/sobjects/Product2/01t36000001To1mAAC" }, "Product_Image_URL__c": null, "Description": "ADA RICE 200 x 30", "Family": null, "Product_Image__c": "" }, "Product2Id": "01t36000001To1mAAC", "Pricebook2": { "attributes": { "type": "Pricebook2", "url": "/services/data/v36.0/sobjects/Pricebook2/01s36000004AGVLAA4" }, "Name": "UK Pricebook" } }] };
var y = json.records;
alert(y);
localStorage.setItem("offlinecurries1", JSON.stringify(y));
var x = JSON.parse(localStorage.getItem("offlinecurries1"));
alert(x[0].Name);