简体   繁体   English

索引到JSON

[英]Indexing into JSON

This seems like it should be a very easy question but I'm having some trouble with it. 看来这应该是一个非常简单的问题,但是我遇到了一些麻烦。 I'm creating my own JSON and I need to index into it in order to seed by database. 我正在创建自己的JSON,并且需要对其进行索引才能通过数据库进行播种。 I've indexed into JSONs before with very little difficulty, but for some reason I can't index into my own. 以前,我几乎没有困难地将索引编入JSON,但是由于某些原因,我无法将索引编入自己的索引。 That makes me think that there might be an issue with my JSON itself, but I can't see anything that would cause an issue. 这使我认为JSON本身可能存在问题,但是我看不到任何会引起问题的东西。 I appreciate your assistance! 感谢您的协助!

My JSON: 我的JSON:

{
    "workouts": [
        {
            "level": "1",
            "exercises": [
                {
                    "name": "box jumps",
                    "difficulty": "3",
                    "reps": "10",
                    "sets": "3",
                    "requirements": [
                        "sturdy box at least two feet high"
                    ],
                    "body-part": "quadriceps",
                    "description": "Plant both feet should length apart and jump onto the box. Once on the box, stand fully upright.",
                    "pounds": "1"
                },
                {
                    "name": "v-press",
                    "difficulty": "4",
                    "reps": "12",
                    "sets": "3",
                    "requirements": [
                        "mat"
                    ],
                    "body-part": "abdominals",
                    "description": "Lie flat on the ground, then raise your legs and arms slightly off the matt.",
                    "pounds": "1"
                }
            ]
        },
        {
            "level": "2",
            "exercises": [
                {
                    "name": "assisted pullups",
                    "difficulty": "1",
                    "reps": "12",
                    "sets": "3",
                    "requirements": [
                        "Assisted Pullup Machine"
                    ],
                    "body-part": "biceps",
                    "description": "Kneel on the machine and adjust the weight to your needs",
                    "pounds": "50"
                },
                {
                    "name": "assisted dips",
                    "difficulty": "1",
                    "reps": "12",
                    "sets": "3",
                    "requirements": [
                        "Assisted Dips Machine"
                    ],
                    "body-part": "triceps",
                    "description": "Kneel on the machine and adjust the weight to your needs",
                    "pounds": "50"
                }
            ]
        }
    ]
}

In pry, I do the following: 撬起后,我执行以下操作:

require "json"
f= File.open("workout.json")
mylist = JSON.parse(f.read)

When I try to index in, I get various errors (syntax error, no method errors, nil). 当我尝试索引时,出现各种错误(语法错误,无方法错误,无)。 Below are some examples of indexing I have attempted. 以下是我尝试建立索引的一些示例。

mylist.workouts
mylist[:workouts]
mylist[0]
mylist[:workouts][0][:level]

Thanks in advance! 提前致谢!

The keys in the Hash after parsing the JSON data are strings not symbols. 解析JSON数据后,哈希中的键是strings而不是符号。 Try this : 尝试这个 :

mylist['workouts']
mylist['workouts'][0]['level']

A couple of points to remember : 需要记住的几点:

  1. Strings and Symbols are not interchangeable as keys in a Hash . 字符串和符号不能作为Hash键互换。 They both are different objects and hence different keys. 它们都是不同的对象,因此是不同的键。
  2. To get the behaviour of the params in Rails controller where strings and symbols are interchangeable you need to instantiate an instance of HashWithInDifferentAccess . 要获得在Rails控制器中params和字符串和符号可互换的行为,您需要实例化HashWithInDifferentAccess的实例。 It is a separate utility class provided by Rails and is not part of the Ruby stdlib 它是Rails提供的独立实用程序类,不是Ruby stdlib的一部分
  3. The gem jbuilder is not a JSON parser. gem jbuilder不是JSON解析器。 It is a JSON creator. 它是JSON创建者。 It is used to create JSON structures from Ruby objects, used mostly in writing views for JSON responses. 它用于从Ruby对象创建JSON结构,主要用于编写JSON响应的视图。 It is analogous to how ERB is used for HTML responses. 它类似于如何将ERB用于HTML响应。
  4. JSON has been part of Ruby stdlib for some time now (ie JSON parsing and serialization does not require any additional gems). JSON已经成为Ruby stdlib的一部分了(即JSON解析和序列化不需要任何其他的gem)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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