简体   繁体   中英

Python parsing json data

I was trying to parse a json file and select some attribute.

here is my code

import json
import urllib

results = json.load(urllib.urlopen("https://www.kimonolabs.com/api/adgmajn2?apikey=L63EvSC1x5vG8iSbm9Jon3784mkDp1Or"))

parse_result  = json.loads(results)
print parse_result 

And here is json data format

{
  "name": "google_test",
  "count": 20,
  "frequency": "Manual Crawl",
  "version": 1,
  "newdata": true,
  "lastrunstatus": "success",
  "thisversionstatus": "success",
  "thisversionrun": "Sun Jun 07 2015 17:19:33 GMT+0000 (UTC)",
  "results": {
    "collection1": [
      {
        "content": "Parse handles everything you need to store data securely and efficiently in the cloud. Store basic data types, including locations and photos, and query across ...",
        "title": {
          "href": "https://parse.com/products/core",
          "text": "Parse Core | Everything your app needs to save data, be ..."
        },
        "index": 1,
        "url": "https://www.google.com.tw/webhp?sourceid=chrome-instant&ion=1&espv=2&es_th=1&ie=UTF-8#/es_th=1&q=pase%20data"
      },
      {
        "content": "Parsing or syntactic analysis is the process of analysing a string of symbols, either ... In order to parse natural language data, researchers must first agree on the  ...",
        "title": {
          "href": "http://en.wikipedia.org/wiki/Parsing",
          "text": "Parsing - Wikipedia, the free encyclopedia"
        },
        "index": 2,
        "url": "https://www.google.com.tw/webhp?sourceid=chrome-instant&ion=1&espv=2&es_th=1&ie=UTF-8#/es_th=1&q=pase%20data"
      },
      {
        "content": "In a data flow, Integration Services sources do the work of extracting data, parsing string data, and converting data to an Integration Services data type.",
        "title": {
          "href": "https://msdn.microsoft.com/en-us/ms141022.aspx",
          "text": "Parsing Data - MSDN - Microsoft"
        }

        ......
    ]
  }

However, it show me some error. I can't understand what is it.

Traceback (most recent call last): File "get_json_data_2.py", line 6, in parse_result = json.loads(results) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/ init .py", line 338, in loads return _default_decoder.decode(s) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: expected string or buffer

results already contains the parsed JSON tree. You don't need the loads part:

import json
import urllib

results = json.load(urllib.urlopen("https://www.kimonolabs.com/api/adgmajn2?apikey=L63EvSC1x5vG8iSbm9Jon3784mkDp1Or"))

print results

You could convert json to yaml using for example this: http://jsontoyaml.com/ . Yaml is python "version" of json.

import yaml

stram = open("test", "r")
print yaml.load(stram)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM