如何使用javascript或jquery读取非标准json文件 [英] how to read non standard json file using javascript or jquery

查看:100
本文介绍了如何使用javascript或jquery读取非标准json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下json文件格式.

I have following json file format.

{
  "browser": "firefox",
  "dateTime": "28_May_2014_03_35_PM"
}
{
  "browser": "firefox",
  "dateTime": "28_May_2014_03_36_PM"
}

根据要求,我无法更改其格式,但我必须阅读此书,我知道我们可以提供[和]花括号并用分隔每个元素,但我不能这样做,我的源文件仅具有上述格式.

as per requirement I cannot change it format, but I have to read this I know we can provide [ and ] braces and separate each element by , but I cant do this, my source file is having above format only.

那我怎么读这样的文件?

so how can I read such file?

是否可以读取此类文件并将整个文件内容转换为有效的json格式的内容?

Is there any way to read such file and convert whole file content to valid json formatted content?

推荐答案

您可以尝试类似的操作

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>

<script>
  debugger;
  var input = '{  "browser": "firefox",  "dateTime": "28_May_2014_03_35_PM"  }  {    "browser": "firefox",    "dateTime": "28_May_2014_03_36_PM"  }';

  input = input.replace('}', '},');
  input = '[' + input + ']';

  var _json = JSON.parse(input);

  for (var i = 0; i < _json.length; i++)
    alert(_json[i].browser);
</script>
</body>
</html>

但是如果您在其中嵌套了对象,将无法正常工作

but if you have nested object inside will not work

这篇关于如何使用javascript或jquery读取非标准json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆