Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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
使用jQuery从JSON字符串中提取值_Jquery_Json - Fatal编程技术网

使用jQuery从JSON字符串中提取值

使用jQuery从JSON字符串中提取值,jquery,json,Jquery,Json,我有这段代码试图从下面的JSON字符串中提取第一个(或两个)“ZoneId”值: var obj = jQuery.parseJSON('{"SecureZoneSubscriptionList": {"EntityId": 8628428,"Subscriptions": [{"ZoneName": "Customer Secure Zone","ZoneId": "51",},{"ZoneName": "Wholesaler Secure Zone","ZoneId": "3573",},]

我有这段代码试图从下面的JSON字符串中提取第一个(或两个)“ZoneId”值:

var obj = jQuery.parseJSON('{"SecureZoneSubscriptionList": {"EntityId": 8628428,"Subscriptions": [{"ZoneName": "Customer Secure Zone","ZoneId": "51",},{"ZoneName": "Wholesaler Secure Zone","ZoneId": "3573",},]}}');
alert(obj.SecureZoneSubscriptionList[0].ZoneId);
我已经看过其他类似版本的代码,它们确实可以做到这一点,但当我将其应用到我的情况时,是不是失败了? 我很想知道我做错了什么(这是我第一次使用JSON,而且还是jQuery的新手)。。。非常感谢您的帮助。 谢谢

  • JavaScript字符串中的某个地方出现了语法错误,我假设这是一个错误的复制粘贴

  • 鉴于此限制,您需要查找
    obj.SecureZoneSubscriptionList.Subscriptions[0].ZoneId
    -请注意其中的
    .Subscriptions

  • JavaScript字符串中的某个地方出现了语法错误,我假设这是一个错误的复制粘贴

  • 鉴于此限制,您需要查找
    obj.SecureZoneSubscriptionList.Subscriptions[0].ZoneId
    -请注意其中的
    .Subscriptions

  • 以下是你的答案:

     var obj = {
         "SecureZoneSubscriptionList": {
             "EntityId": 8628428,
             "Subscriptions": [{
                 "ZoneName": "Customer Secure Zone",
                 "ZoneId": "51",
                 },
             {
                 "ZoneName": "Wholesaler Secure Zone",
                 "ZoneId": "3573",
                 }, ]
         }
     };
    
     alert(obj.SecureZoneSubscriptionList.Subscriptions[0].ZoneId);
    
    请注意,“订阅”是数组所在的位置。。。非SecureZoneSubscriptionList

    编辑问题盖伊问了另一个问题:

      var obj = {
          "SecureZoneSubscriptionList": {
          "EntityId": 8628428,
               "Subscriptions": [{
                    "ZoneName": "Customer Secure Zone",
                    "ZoneId": "51",
               } ]
          }
     };
    
     alert(obj.SecureZoneSubscriptionList.Subscriptions[0].ZoneId);
    
    这仍然适用于1个元素

     alert(obj.SecureZoneSubscriptionList.Subscriptions.length);
    
    上面将告诉您元素的长度。您可以使用一些条件语句来
    if/else if/else

    以下是您的答案:

     var obj = {
         "SecureZoneSubscriptionList": {
             "EntityId": 8628428,
             "Subscriptions": [{
                 "ZoneName": "Customer Secure Zone",
                 "ZoneId": "51",
                 },
             {
                 "ZoneName": "Wholesaler Secure Zone",
                 "ZoneId": "3573",
                 }, ]
         }
     };
    
     alert(obj.SecureZoneSubscriptionList.Subscriptions[0].ZoneId);
    
    请注意,“订阅”是数组所在的位置。。。非SecureZoneSubscriptionList

    编辑问题盖伊问了另一个问题:

      var obj = {
          "SecureZoneSubscriptionList": {
          "EntityId": 8628428,
               "Subscriptions": [{
                    "ZoneName": "Customer Secure Zone",
                    "ZoneId": "51",
               } ]
          }
     };
    
     alert(obj.SecureZoneSubscriptionList.Subscriptions[0].ZoneId);
    
    这仍然适用于1个元素

     alert(obj.SecureZoneSubscriptionList.Subscriptions.length);
    

    上面将告诉您元素的长度。您可以使用一些条件语句来
    if/else if/else

    假设不是输入错误,您的JSON是无效的(您有很多不应该出现的逗号)。是你的朋友,我建议你在遇到这样的问题时使用它

    {
        "SecureZoneSubscriptionList": {
            "EntityId": 8628428,
            "Subscriptions": [
                {
                    "ZoneName": "Customer Secure Zone",
                    "ZoneId": "51",
    
                },
                {
                    "ZoneName": "Wholesaler Secure Zone",
                    "ZoneId": "3573",
    
                },
    
            ]
        }
    }
    

    正确:

    {
        "SecureZoneSubscriptionList": {
            "EntityId": 8628428,
            "Subscriptions": [
                {
                    "ZoneName": "Customer Secure Zone",
                    "ZoneId": "51"
                },
                {
                    "ZoneName": "Wholesaler Secure Zone",
                    "ZoneId": "3573"
                }
            ]
        }
    }
    

    假设它不是输入错误,那么JSON就无效(您有很多不应该出现的逗号)。是你的朋友,我建议你在遇到这样的问题时使用它

    {
        "SecureZoneSubscriptionList": {
            "EntityId": 8628428,
            "Subscriptions": [
                {
                    "ZoneName": "Customer Secure Zone",
                    "ZoneId": "51",
    
                },
                {
                    "ZoneName": "Wholesaler Secure Zone",
                    "ZoneId": "3573",
    
                },
    
            ]
        }
    }
    

    正确:

    {
        "SecureZoneSubscriptionList": {
            "EntityId": 8628428,
            "Subscriptions": [
                {
                    "ZoneName": "Customer Secure Zone",
                    "ZoneId": "51"
                },
                {
                    "ZoneName": "Wholesaler Secure Zone",
                    "ZoneId": "3573"
                }
            ]
        }
    }
    

    这里有几个额外的逗号,JSON规范中不支持额外的逗号。例如:
    “ZoneId”:“3573”,},]}
    (在括号和大括号之前)。只要它们仍然存在,您就会遇到困难。

    您在那里有几个额外的逗号,JSON规范中不支持额外的逗号。例如:
    “ZoneId”:“3573”,},]}
    (在括号和大括号之前)。只要这些仍然存在,您就会遇到困难。

    而您得到的错误是。。。。?让我猜猜,
    obj为null
    ?另外,FireBug报告了以下错误:JSON.parse:应该是双引号属性名,而您得到的错误是。。。。?让我猜猜,
    obj为null
    ?为了添加,FireBug报告了以下错误:JSON.parse:应该是双引号属性nameHi,再次。我遇到了这个问题。有时我的JSON数据只有一个订阅数据集,有时它有两个订阅数据集。当我有代码提取“订阅[0]”和订阅[1]时,如果只有一个订阅数据集,脚本将出错。我是否可以检索可能存在的任何/所有“ZoneId”值?换句话说,我是否可以不定义要查找的特定索引(请原谅任何不正确的术语)是的,
    .length
    if/else
    语句结合使用就达到了目的。谢谢你,你是我的救星。嗨,又来了。我遇到了这个问题。有时我的JSON数据只有一个订阅数据集,有时它有两个订阅数据集。当我有代码提取“订阅[0]”和订阅[1]时,如果只有一个订阅数据集,脚本将出错。我是否可以检索可能存在的任何/所有“ZoneId”值?换句话说,我是否可以不定义要查找的特定索引(请原谅任何不正确的术语)是的,
    .length
    if/else
    语句结合使用就达到了目的。谢谢你,你是我的救星。