【问题标题】:convert a server response string to a valid JSON using javascript使用 javascript 将服务器响应字符串转换为有效的 JSON
【发布时间】:2018-04-06 03:22:02
【问题描述】:

我无法控制修改服务器端点以返回有效的 JSON。

端点:http://newsrack.in/stories/servelots/iihs_feeds/16.json

示例响应数据:

    `var _nr_metadata = {
      site_base_url : "http://newsrack.in",
      issue_name    : "iihs_feeds",
      category_name : "Chikungunya",
      listing_url   : "/stories/servelots/iihs_feeds/16"
    }
    var _nr_stories = [
       {
    title  : "Delhi: Changing weather conditions cause 25 per cent rise in dengue cases",
    url    : "http://indiatoday.intoday.in/story/delhi-changing-weather-conditions-cause-rise-in-dengue-cases/1/1075570.html",
    source : "India Today| Must Read",
    date   : "26.10.2017",
    desc   : "<a href=\'http://indiatoday.intoday.in/story/delhi-changing-weather-conditions-cause-rise-in-dengue-cases/1/1075570.html?utm_source=rss\'><img src=\'http://media2.intoday.in/indiatoday/images/stories/dengue305_102617022722.jpg\' align=\"left\" hspace=\"2\" height=\"180\" width=\"305\" alt=\"\" border=\"0\"/><\/a>Usually at this time of the year, the virus becomes inactive due to \ntemperature dip. But experts are witnessing the hostile nature of ades \nmosquitoes."
  },
  {
    title  : "Waste management bye-laws pending approval of LG: Delhi High Court told",
    url    : "http://indianexpress.com/article/delhi/waste-management-bye-laws-pending-approval-of-lg-delhi-high-court-told-4906249/",
    source : "Delhi – The Indian Express",
    date   : "25.10.2017",
    desc   : "<img alt=\"\" border=\"0\" src=\"http://pixel.wp.com/b.gif?host=indianexpress.com&#038;blog=53855017&#038;post=4906249&#038;subd=indianexpressonline&#038;ref=&#038;feed=1\" width=\"1\" height=\"1\" />"
  },
  {
    title  : "Alarm bells ringing:194 dengue cases in 2 weeks in district",
    url    : "http://www.tribuneindia.com/news/ludhiana/alarm-bells-ringing-194-dengue-cases-in-2-weeks-in-district/486718.html",
    source : "The Tribune",
    date   : "25.10.2017",
    desc   : "Tribune News Service\nLudhiana, October 24\nThe number of dengue cases is rapidly increasing in the district as 194 confirmed cases have been recorded by the Health Department in the past two weeks.\nA total of 309 confirmed cases and 524 suspected cases of dengue have been reported in the district this year till Monday. According to the Health Department, cases are mostly being reported from the areas on Chandigarh Road in Ludhiana. These include 33-foot Road, GTB Nagar, Mundian Kalan, Guru Nanak Nagar, GK Estate, Jamalpur, Sectors 32 and 39. There are chances that the number of dengue cases could be higher than official reports, say residents.\nThe department had recorded 31 confirmed dengue cases till September 22 and 115 cases till October 10 in these places. Apart from these cases, as many as 10 confirmed cases of chikungunya, which is also spread by bite of infected aedes mosquitoes, have been reported here this year.\nHealth team finds mosquito larvae in 438 containers\nHealth Inspector Manpreet Singh ..."
  },
  {
    title  : "Alarm bells ringing:194 dengue cases in 2 weeks in district",
    url    : "http://www.tribuneindia.com/news/ludhiana/alarm-bells-ringing-194-dengue-cases-in-2-weeks-in-district/486718.html",
    source : "The Tribune",
    date   : "25.10.2017",
    desc   : "Tribune News Service\nLudhiana, October 24\nThe number of dengue cases is rapidly increasing in the district as 194 confirmed cases have been recorded by the Health Department in the past two weeks.\nA total of 309 confirmed cases and 524 suspected cases of dengue have been reported in the district this year till Monday. According to the Health Department, cases are mostly being reported from the areas on Chandigarh Road in Ludhiana. These include 33-foot Road, GTB Nagar, Mundian Kalan, Guru Nanak Nagar, GK Estate, Jamalpur, Sectors 32 and 39. There are chances that the number of dengue cases could be higher than official reports, say residents.\nThe department had recorded 31 confirmed dengue cases till September 22 and 115 cases till October 10 in these places. Apart from these cases, as many as 10 confirmed cases of chikungunya, which is also spread by bite of infected aedes mosquitoes, have been reported here this year.\nHealth team finds mosquito larvae in 438 containers\nHealth Inspector Manpreet Singh ..."
  },
  {
    title  : "650 new cases of dengue, 48 of chikungunya",
    url    : "http://www.thehindu.com/news/cities/Delhi/650-new-cases-of-dengue-48-of-chikungunya/article19908528.ece",
    source : "Hindu: Cities",
    date   : "24.10.2017",
    desc   : "More than 1,000 dengue cases reported so far this month"
  },
  '' // Last item -- needed because previous item ends with a comma
]`

如您所见,示例数据不是有效的 JSON,我尝试了以下功能,但最终在键中出现了不必要的空间,这也是一个问题。 `

//Step 1
        function extractjson(strarg){
                var found = [],          // an array to collect the strings that are found
                    rxp = /{([^}]+)}/g,

                    curMatch;
var parsed=[];
                    // step 2: regex to add quotes
                    var objKeysRegex = /({|,)(?:\s*)(?:')?([A-Za-z_$\.][A-Za-z0-9_ \-\.$]*)(?:')?(?:\s*):/g;
                    while( curMatch = rxp.exec( strarg ) ) {

                        found.push( curMatch[0].replace(objKeysRegex, "$1\"$2\":") );
                    }

                //step 3- parse the found data
                for(i=0;i<found.length;i++){
                    try {
                      json = JSON.parse(found[i]);
                    } catch (exception) {
                      json = null;
                    }
                if (json) {

                  //the json is ok
                parsed.push(JSON.parse(found[i]));

                }else{
                console.log("badjson");
      //the json is not ok

                }
            }
            console.log("input length =", found.length, "output length=", parsed.length);

            return parsed;
            }
            }

`

【问题讨论】:

  • 我不愿意使用 eval() 因为这将用于生产应用程序,需要一个更安全的解决方案。
  • 我的回答对你有帮助吗?
  • @JustinPowell 不,我无法使用您的代码获得有效的 JSON,我已经更新了失败的数据,如果可以请提供帮助
  • 我明白了,自从我上次尝试以来,数据发生了变化。新问题是数据中存在不正确转义的撇号,我添加了一行来处理这些问题。
  • 是的,数据每天都在变化,因为它是一个提供 xml 和 json 输出的新闻聚合站点,您的解决方案正在运行,我会尽快确认

标签: javascript json regex


【解决方案1】:

这是一个相当幼稚的解析尝试……它对数据的格式做了一些假设(例如,它在变量名上查找 _nr_ 前缀)。我建议将字符串拆分为各种 var 声明,然后在使用 JSON.parse 评估之前清理它们之间的每个数据部分。

let parseJSON = (text) => {
  let quoteKeysAndParse = (text) => {
     //Quote keys in objects
     let quoted = text.replace(/([\{\[,]\s*)(['"])?([a-zA-Z0-9_]+)(['"])?\s*:/g, '$1"$3": ');
     //Remove the "last item" text
     quoted = quoted.replace(/,\s+'' \/\/ Last item[^\]^}]+([\]\}])/g, '$1');
     //Remove improperly escaping of apostrophes
     quoted = quoted.replace(/([^\\])\\'/g, '$1\'');
     //Parse the JSON
     return JSON.parse(quoted);
  }
  
  //Find var declarations
  let declarations = text.match(/var\s+_nr_[^\s]+\s+=\s+/ig), obj = {}, key = null, prevKey = null;
  text = ['',text];
  //For each variable...
  for(let declaration of declarations){
    key = declaration.match(/_nr_[^\s]+/)[0];
    let currentText = text[1].split(declaration);
    text = currentText;
    if(prevKey){
      //Parse the prior split section
      obj[prevKey] = quoteKeysAndParse(text[0]);
    }
    prevKey = key;
  }
  
  //Make sure we process the last section
  if(prevKey){
    obj[prevKey] = quoteKeysAndParse(text[1]);
  }
  return obj;
}

fetch('https://cors-anywhere.herokuapp.com/newsrack.in/stories/servelots/iihs_feeds/16.json')
.then(response => response.text())
.then(parseJSON)
.then(data => {
   for(let item of data._nr_stories){
      let div = document.createElement('div');
      div.innerHTML = `<h3>${item.date} - <a href="${item.url}">${item.title}</a> (${item.source})</h3><p>${item.desc}</p>`;
      document.body.append(div)
   }
});

【讨论】:

  • 这成功了!我将测试我所有的测试用例并很快确认。
【解决方案2】:

这是一个可行的简短解决方案

  // ----------------------- DATA -----------------------
  let code = `
  var _nr_metadata = {
    site_base_url : "http://newsrack.in",
    issue_name    : "iihs_feeds",
    category_name : "Chikungunya",
    listing_url   : "/stories/servelots/iihs_feeds/16"
  }
  var _nr_stories = [
    {
      title  : "Alarm bells ringing:194 dengue cases in 2 weeks in district",
      url    : "http://www.tribuneindia.com/news/ludhiana/alarm-bells-ringing-194-dengue-cases-in-2-weeks-in-district/486718.html",
      source : "The Tribune",
      date   : "25.10.2017",
      desc   : "Tribune News Service\nLudhiana, October 24\nThe number of dengue cases is rapidly increasing in the district as 194 confirmed cases have been recorded by the Health Department in the past two weeks.\nA total of 309 confirmed cases and 524 suspected cases of dengue have been reported in the district this year till Monday. According to the Health Department, cases are mostly being reported from the areas on Chandigarh Road in Ludhiana. These include 33-foot Road, GTB Nagar, Mundian Kalan, Guru Nanak Nagar, GK Estate, Jamalpur, Sectors 32 and 39. There are chances that the number of dengue cases could be higher than official reports, say residents.\nThe department had recorded 31 confirmed dengue cases till September 22 and 115 cases till October 10 in these places. Apart from these cases, as many as 10 confirmed cases of chikungunya, which is also spread by bite of infected aedes mosquitoes, have been reported here this year.\nHealth team finds mosquito larvae in 438 containers\nHealth Inspector Manpreet Singh ..."
    },
    {
      title  : "650 new cases of dengue, 48 of chikungunya",
      url    : "http://www.thehindu.com/news/cities/Delhi/650-new-cases-of-dengue-48-of-chikungunya/article19908528.ece",
      source : "Hindu: Cities",
      date   : "24.10.2017",
      desc   : "More than 1,000 dengue cases reported so far this month"
    },
  ]
  `

  // --------------------- / DATA ------------------------


  // --------------------- TREATMENT ------------------------

  
  // Add '"' around the property names
  
  code = code.replace(/\n +([a-z_]+) +:/g, '"$1" :').replace(/[\n\r]/g,'');

  // To avoid unecessary ',' in "}, ]"
  
  code = code.replace(/\},[ ]*\]/g,'}]');


  // Split by var (keeping var name)
  
  code = code.split(/ var(?=[a-z_ ]+= [\[\{])/);
    
  // The result to feed
    
  let result = {} ;

  // Treat each var
  
  code.forEach( d => {

    d = d.trim();
    if (!d) { return }
    
    // Separate var name and var value
    // Faster?
    let index = d.indexOf('=');
    let var_name = d.substr(0, index).trim();
    let var_value = d.substr(index+1, d.length).trim();

    // Parse the value
    
    try 
    {
      result[var_name] = JSON.parse(var_value);
    } 
    catch(error)
    {
      console.error(error);
    }
  })
  
  console.log(result);

【讨论】:

  • 我喜欢这种方法,我想我错过了我的问题的一小部分,响应的结尾也有一个空字符串,因此JSON.parse() 失败,下面是数据的结尾来自回复,{ &lt;data ... &gt; }, ' ' // Last item -- needed because previous item ends with a comma ]单引号和评论行剧透
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-08
  • 2012-04-07
  • 2012-10-14
  • 2016-07-01
  • 2018-12-17
  • 2018-08-29
  • 1970-01-01
相关资源
最近更新 更多