I'm in over my head, trying to parse JSON for my first time and dealing with a multi dimensional array.

我在我脑海中,试图第一次解析JSON并处理多维数组。

{
  "secret": "[Hidden]",
  "minutes": 20,
  "link": "http:\/\/www.1.com",
  "bookmark_collection": {
    "free_link": {
      "name": "#free_link#",
      "bookmarks": [
        {
          "name": "1",
          "link": "http:\/\/www.1.com"
        },
        {
          "name": "2",
          "link": "http:\/\/2.dk"
        },
        {
          "name": "3",
          "link": "http:\/\/www.3.in"
        }
      ]
    },
    "boarding_pass": {
      "name": "Boarding Pass",
      "bookmarks": [
        {
          "name": "1",
          "link": "http:\/\/www.1.com\/"
        },
        {
          "name": "2",
          "link": "http:\/\/www.2.com\/"
        },
        {
          "name": "3",
          "link": "http:\/\/www.3.hk"
        }
      ]
    },
    "sublinks": {
      "name": "sublinks",
      "link": [
        "http:\/\/www.1.com",
        "http:\/\/www.2.com",
        "http:\/\/www.3.com"
      ]
    }
  }
}

This is divided into 3 parts, the static data on my first dimension (secret, minutes, link) Which i need to get as seperate strings.

这分为3个部分,我的第一个维度上的静态数据(秘密,分钟,链接)我需要作为单独的字符串获取。

Then I need a dictionary per "bookmark collection" which does not have fixed names, so I need the name of them and the links/names of each bookmark.

然后我需要一个没有固定名称的“书签集”字典,所以我需要它们的名称和每个书签的链接/名称。

Then there is the seperate sublinks which is always the same, where I need all the links in a seperate dictionary.

然后是单独的子链接,它总是相同的,我需要单独的字典中的所有链接。

I'm reading about parsing JSON but most of the stuff I find is a simple array put into 1 dictionary. Does anyone have any good techniques to do this ?

我正在阅读有关解析JSON的内容,但我发现的大多数内容都是放入1个字典中的简单数组。有没有人有任何好的技巧来做到这一点?

2 个解决方案

#1


11

After you parse the JSON, you will end up with a Python dict. So, suppose the above JSON is in a string named input_data:

在解析JSON之后,最终会得到一个Python dict。因此,假设上面的JSON位于名为input_data的字符串中:

import json
# This converts from JSON to a python dict
parsed_input = json.loads(input_data)

# Now, all of your static variables are referenceable as keys:
secret = parsed_input['secret']
minutes = parsed_input['minutes']
link = parsed_input['link']

# Plus, you can get your bookmark collection as:
bookmark_collection = parsed_input['bookmark_collection']

# Print a list of names of the bookmark collections...
print bookmark_collection.keys() # Note this contains sublinks, so remove it if needed

# Get the name of the Boarding Pass bookmark:
print bookmark_collection['boarding_pass']['name']

# Print out a list of all bookmark links as:
#  Boarding Pass
#    * 1: http://www.1.com/
#    * 2: http://www.2.com/
#  ...
for bookmark_definition in bookmark_collection.values():
    # Skip sublinks...
    if bookmark_definition['name'] == 'sublinks':
        continue
    print bookmark_definition['name']
    for bookmark in bookmark_definition['bookmarks']:
        print "    * %(name)s: %(link)s" % bookmark

# Get the sublink definition:
sublinks = parsed_input['bookmark_collection']['sublinks']

# .. and print them
print sublinks['name']
for link in sublinks['link']:
    print '  *', link

更多相关文章

  1. Python 3.4中的Pytesser:名称“image_to_string”没有定义?
  2. 使用nasm和ld汇编/链接问题
  3. 解决nfs链接开发板出现:nfs:server is not responding,still tryi
  4. 获取网卡名称 linux c
  5. 我应该在哪里添加Yocto位烤任务来创建工作文件夹符号链接?
  6. Linux中动态链接库总结
  7. 删除目录软链接注意事项
  8. 求GridControl_11.1.0.1.0_Linux_x86-64_1of3 的下载链接最好是
  9. 用GCC编译链接程序--编译链接器GCC常用功能(菜鸟级)

随机推荐

  1. 【图解数据结构】 一组动画彻底理解基数
  2. 【图解数据结构】 一组动画演示冒泡排序
  3. 每天一算:Reverse Linked List
  4. 每天一算:Reverse Linked List II
  5. 国内最具影响力科技创投媒体36Kr的容器化
  6. 十大经典排序算法动画,看我就够了!
  7. Mybatis002-CRUD
  8. 这些奇葩的排序算法,你没见过动画吧?
  9. 从简单的线性数据结构开始:栈与队列
  10. 看动画轻松理解时间复杂度(一)