I did something stupid... I gathered a big tonne of json data and saved it in one file. I now find myself getting errors when I try JSON.parse(file) due to the fact that its JSON objec开发者_如何学编程t after JSON object. Can anyone advise on how I can parse this data? The strcture looks as follows. The next entry is a object with the exact same structure.
{"purchase":
{
"amount": 34.595399,
"uid": 1282907706,
"user":
{
"id": xxxx
"name": "xxxx"
},
"dailycount": 135.82373100000001,
"productdetails":
{
"type": "shoes"
},
"details":
{
"gender": "male"
},
"createdin": "Asia/Tokyo",
"id": 147707740,
"comments": []}}
The problem, of course is that you dont have a valid json object.
irb(main):004:0> JSON.parse("{'foo':'bar'},{'foo':'baz'}")
JSON::ParserError: 705: unexpected token at '{'foo':'bar'},{'foo':'baz'}'
from /usr/lib/ruby/gems/1.8/gems/json-1.4.6/lib/json/common.rb:146:in `parse'
from /usr/lib/ruby/gems/1.8/gems/json-1.4.6/lib/json/common.rb:146:in `parse'
from (irb):4
So, I would add a '"key": [' and a ']' before the end of your last curly brace. to make it valid json.
irb(main):018:0> json = '{"key" : [{"foo":"bar"}, {"foo":"baz"}]}'
=> "{\"key\" : [{\"foo\":\"bar\"}, {\"foo\":\"baz\"}]}"
irb(main):019:0> JSON.parse json
=> {"key"=>[{"foo"=>"bar"}, {"foo"=>"baz"}]}
Could you use the Search & Replace in your text editor? I mean, if you have a chunk of code like this:
{
"a" : "b",
"c" : "d"
}
{
"a" : "e",
"c" : "f",
}
You may turn it by searching for the }\s*{
sequence and replacing with the }, {
sequence to something like this:
[
{
"a" : "b",
"c" : "d"
},
{
"a" : "e",
"c" : "f",
}
]
精彩评论