最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - How to loop through newline separated json line by line in JavaScript? - Stack Overflow

programmeradmin2浏览0评论

I have a JSON file in my project that looks like this:

{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}

I'm not sure how to loop through each line to put it in an array. How can I do this?

I have a JSON file in my project that looks like this:

{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}

I'm not sure how to loop through each line to put it in an array. How can I do this?

Share Improve this question edited Mar 20, 2017 at 13:29 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Mar 9, 2017 at 23:56 user1261710user1261710 2,6396 gold badges45 silver badges76 bronze badges 7
  • Are you able to open the file and read it in? – Ken H. Commented Mar 9, 2017 at 23:59
  • 1 So you want to take an invalid structure and turn it into an Array of objects. Load the file, split new lines, join with comma, wrap with [ and ], and JSON.parse it. OR fix it so it is valid to start and not have to deal with that. – epascarello Commented Mar 10, 2017 at 0:01
  • 3 @epascarello Encapsulating one format in another is very common. Line-delimited JSON streams are extremely common. It's probably the best, most straightforward way to handle a stream of JSON objects. Would you rather have an infinitely sized never ending array and buffer data into memory forever? Would you rather hack the parser or implement a streaming parser? There's nothing wrong with line-delimited JSON as long as you know what you're doing and have a good use case for it. – Brad Commented Mar 10, 2017 at 0:06
  • @Brad sounds like there is not a good use case for this if OP needs it in an array. ;) – epascarello Commented Mar 10, 2017 at 0:08
  • You need some way of parsing this as a javascript object. If you're using a backend like node try using fs if you're using javascript in the browser, you'll have to use some ajax request. – richbai90 Commented Mar 10, 2017 at 0:11
 |  Show 2 more comments

2 Answers 2

Reset to default 10

Split on new lines, join with comma, wrap it with brackets, and parse it.

var str = `{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}`

var lines = str.split(/\n/);
var wrapped = "[" + lines.join(",") + "]";
var obj = JSON.parse(wrapped);

console.log(obj);

Better solution, fix whatever gives you that format to give you the correct structure to start out with.

What about using map properly?

var myVar = `{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}`;

var myArray = myVar.split('\n').map(JSON.parse);
console.log(myArray);

发布评论

评论列表(0)

  1. 暂无评论