简体   繁体   English

Javascript-如何遍历无效的JSON

[英]Javascript- How to loop through invalid JSON

I have a JSON array like this 我有这样的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
   }
}

this JSON is invalid but I am getting it in this form, I cant get it to parse as it is not a valid JSON and I cannot remove special characterd from the string. 这个JSON是无效的,但是我在这种形式下得到它,我无法解析,因为它不是一个有效的JSON,我不能从字符串中删除特殊字符。 How can I extract the data of specific index from this invalid JSON? 如何从这个无效的JSON中提取特定索引的数据? any way to loop through this JSON? 循环这个JSON的任何方法?

With the example that you have given you could simply remove all the line breaks from your string, resulting in valid JSON. 通过您给出的示例,您可以简单地从字符串中删除所有换行符,从而生成有效的JSON。

I did not test it, but it's probably something like this: 我没有测试它,但它可能是这样的:

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

Modified from https://stackoverflow.com/a/5664521/2911452 修改自https://stackoverflow.com/a/5664521/2911452

Good luck. 祝好运。

Update 更新

As Emissary mentioned, removing new lines results in data loss. 正如使者提到的那样,删除新行会导致数据丢失。 Therefore you could replace the new lines with "JSON escaped new lines". 因此,您可以使用“JSON转义的新行”替换新行。 See: https://stackoverflow.com/a/42073/2911452 请参阅: https//stackoverflow.com/a/42073/2911452

I would just replace the offending line endings 我只会替换有问题的行结尾

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM