python - No access to top level elements with ijson? -
I am trying to parse a JSON file with Python's ijson
library. It works when viewing second-tier elements or using a parser, but I like the convenience of watching the top-level elements directly.
JSON is basically formatted like this: {"foo": {"one": 1, "b": 2}, "bar": { "C": 3, "D": 4}}
Then, nothing what I would like to do fancy, through this, come again in such a way that "foo" / Code> and its value is generated by its value /
"bar"
and repetition copy. (As I was for this code.) My code is low - I know that it is not both going to be produced, I was just testing:
f = open ('test') i = ijson.items (f, 'item') in j: print j
item syntax ( )
is the function.
However, oddly, the loop takes time, but it does not really output anything. (Even if I put any print 'qyx'
in it, I do not know what it is doing.) If I type the item ()
function I modify the code to parse foo.item
, it works, but the small information that I can get is indicating that it should also work for the top level. Plus, if I use the following, it works, but it is more opaque:
i = ijson.parse (f) For the prefix, the value in the event i, if not Prefix or event ==
this will output
pre> foo start_map None foo.a no.1 foo.b no. 2 foo end_map none bar start_map none bar.c number 3 bar.d number 4 times end_map none
.. which Can be processed to generate "foo"
and ("a": 1, "b": 2}
etc. This is more of a problem.
Try it:
f = 'Open' ('test') json_obj = Ijson.items (f, ''). Next () # '' Everything loads only as an object in Json_obj.items () for (key, value): Print key + "- & gt;" + str (value)
Comments
Post a Comment