按名称获取JSON对象值 [英] Getting JSON object values by name

查看:80
本文介绍了按名称获取JSON对象值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有效的JSON对象:

I have a valid JSON object like this:

{
    "reasons": {
        "options": [
            {
                "value": "",
                "label": "Choose a reason",
                "selected": true,
                "requiresValidation": false
            },
            {
                "value": "small",
                "label": "Too little",
                "selected": false,
                "requiresValidation": false
            },
            {
                "value": "big",
                "label": "Too big",
                "selected": false,
                "requiresValidation": false
            },
            {
                "value": "unsuitable",
                "label": "I don't like it",
                "selected": false,
                "requiresValidation": true
            },
            {
                "value": "other",
                "label": "Other",
                "selected": false,
                "requiresValidation": true
            }
        ]
    }
}

我有一个存储一个值的变量(例如不合适的选项中提供的选项。
如何检索存储在变量中的值的 requiresValidation 字段的值,而不必遍历中的所有对象值选项

and I have a variable which stores one value (e.g. unsuitable) of an option available in options. How can I retrieve the value of requiresValidation field for the value stored in the variable without having to loop through all of the objects values inside options?

例如,如果var内容是其他我想访问 requireValidation 对象的字段,其值为其他 true )。可能吗?
谢谢。

For instance, if the var content is other I'd like to access to requireValidation field of the object whose value is other (which is true). Is it possible? Thank you.

推荐答案

这里你并没有真正处理JSON,你正在处理一个JS对象。 JSON只是一种发送JS对象的格式。

You aren't really dealing with JSON here, you are dealing with a JS object. JSON is just a format for sending JS objects.

options 是一个数组。访问它的唯一方法是通过索引,这意味着您必须一次搜索一个项目。有一些函数,例如 indexOf(),它将返回数组中值的第一个索引,但是,你有一个对象数组,因此无法使用这个案例。 (在内部,它仍然在搜索)。

options is an array. The only way to access it is by index, which means you will have to do a search, one item at a time. There are functions, such as indexOf() which will return the first index of a value in an array, however, you have an array of objects, so that will not work in this case. (And internally, it is still doing a search).

function getReqVal(val) {
    for (var item in mydata.reasons.options) {
       if(item.value == val) {
           return item.requiresValidation;
       }
    }
}

getReqVal("other");

需要注意的是,这将返回第一个,所以如果你有多个其他,你不会得到它们。

The caveat is that this will return on the first one, so if you have more than one other, you won't get them.

如果选项确实是唯一值,我会重新排列你的对象为一个关联数组,其中键是值项,值是与其余数据对象的值:

If the options are indeed unique values, I would rearrange your object to be an associative array, with the keys being the "value" items, and the values being an object with the rest of the data:

{
    "reasons": {
        "options": {
            "" : {
                "label": "Seleziona una voce",
                "selected": true,
                "requiresValidation": false
            },
            "small" : {
                "label": "Too little",
                "selected": false,
                "requiresValidation": false
            },
            "big" : {
                "label": "Too big",
                "selected": false,
                "requiresValidation": false
            },
            "unsuitable" : {
                "label": "I don't like it",
                "selected": false,
                "requiresValidation": true
            },
            "other" : {
                "label": "Other",
                "selected": false,
                "requiresValidation": true
            }
        }
    }
}

这篇关于按名称获取JSON对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆