Javascript-如何遍历无效的JSON

时间:2016-03-31 18:54:12

标签: javascript json

我有像这样的JSON数组

       {  
   "2070":{  
      "address_id":"2070",
      "firstname":"Simon",
      "lastname":"Hall",
      "company":"",
      "address_1":"",
      "address_2":"",
      "postcode":"44000",
      "city":"",
      "zone_id":"0",
      "zone":"",
      "zone_code":"",
      "country_id":"223",
      "country":"United States",
      "iso_code_2":"US",
      "iso_code_3":"USA",
      "address_format":"{firstname} {lastname} 
    {company} 
    {address_1} 
    {address_2} 
    {city}, {zone} {postcode} 
    {country}",
      "custom_field":false
   },
   "2071":{  
      "address_id":"2071",
      "firstname":"Simon",
      "lastname":"Hall",
      "company":"",
      "address_1":"TEST",
      "address_2":"",
      "postcode":"44000",
      "city":"New York",
      "zone_id":"3625",
      "zone":"Colorado",
      "zone_code":"CO",
      "country_id":"223",
      "country":"United States",
      "iso_code_2":"US",
      "iso_code_3":"USA",
      "address_format":"{firstname} {lastname} 
    {company} 
    {address_1} 
    {address_2} 
    {city}, {zone} {postcode} 
    {country}",
      "custom_field":false
   }
}

这个JSON是无效的,但是我以这种形式得到它,我无法解析,因为它不是有效的JSON,我无法从字符串中删除特殊字符。 如何从这个无效的JSON中提取特定索引的数据? 循环这个JSON的任何方法?

2 个答案:

答案 0 :(得分:2)

使用您提供的示例,您只需从字符串中删除所有换行符,即可生成有效的JSON。

我没有测试它,但它可能是这样的:

var json = "your json";
json = json.replace(/(?:\r\n|\r|\n)/g, '');

https://web.archive.org/web/20131218022035/http://java3d.nl/Tutorials/Java/Java3d/Controlthecamera_12.php修改

祝你好运。

更新

正如使者提到的那样,删除新行会导致数据丢失。因此,您可以使用“JSON转义的新行”替换新行。请参阅:https://stackoverflow.com/a/5664521/2911452

答案 1 :(得分:1)

我只会替换有问题的行结尾



data = data.replace(/\{(lastname|company|address_1|address_2|postcode)\}\s?\n/ig,'{$1}\\\\n');
console.log(JSON.parse(data)); // Object {2070: Object, 2071: Object}

<!-- The following just mocks the response data provided -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text" id="data">
{  
   "2070":{  
      "address_id":"2070",
      "firstname":"Simon",
      "lastname":"Hall",
      "company":"",
      "address_1":"",
      "address_2":"",
      "postcode":"44000",
      "city":"",
      "zone_id":"0",
      "zone":"",
      "zone_code":"",
      "country_id":"223",
      "country":"United States",
      "iso_code_2":"US",
      "iso_code_3":"USA",
      "address_format":"{firstname} {lastname} 
    {company} 
    {address_1} 
    {address_2} 
    {city}, {zone} {postcode} 
    {country}",
      "custom_field":false
   },
   "2071":{  
      "address_id":"2071",
      "firstname":"Simon",
      "lastname":"Hall",
      "company":"",
      "address_1":"TEST",
      "address_2":"",
      "postcode":"44000",
      "city":"New York",
      "zone_id":"3625",
      "zone":"Colorado",
      "zone_code":"CO",
      "country_id":"223",
      "country":"United States",
      "iso_code_2":"US",
      "iso_code_3":"USA",
      "address_format":"{firstname} {lastname} 
    {company} 
    {address_1} 
    {address_2} 
    {city}, {zone} {postcode} 
    {country}",
      "custom_field":false
   }
}
  </script>
<script>var data = $("#data").html();</script>
&#13;
&#13;
&#13;