简体   繁体   English

访问JSON对象以获取Resource Bundle键/值对

[英]Access JSON object to get Resource Bundle key/value pair

I know how to access key value pair from JSON object but in my case, the resource bundle keys are mapped to values. 我知道如何从JSON对象访问键值对,但是在我的情况下,资源束键被映射到值。

eg 例如

var json = {"label.name.first":"foo","label.name.second":"bar"};

Here json.label.name.first doesn't give me "foo". 在这里json.label.name.first不会给我“ foo”。

Can someone help me with this? 有人可以帮我弄这个吗?

Due to using the period character ( . ) in the key name, you need to use the [] notation to access its value. 由于键名中使用句点字符( . ),因此需要使用[]表示法来访问其值。

console.log( json['label.name.first'] );

Additionally, you have a JavaScript object, not JSON. 此外,您有一个JavaScript对象,而不是JSON。


The difference between a JavaScript object or JSON is that JSON is always a string. JavaScript对象或JSON之间的区别在于JSON始终是字符串。 Secondly, JavaScript objects don't require the same quote standards on the key names. 其次,JavaScript对象的键名不需要相同的引用标准。

If you just consider the string below, then yes it can be considred JSON (this is why if you paste it into a JSON parser, it tells you it's valid JSON): 如果仅考虑下面的字符串,则可以将其视为JSON(这就是为什么如果将其粘贴到JSON解析器中,它会告诉您它是有效的JSON):

{"label.name.first":"foo","label.name.second":"bar"}

However, if you assign that directly to a JavaScript variable then you have a JavaScript object literal, not JSON. 但是,如果直接将其分配给JavaScript变量,则将具有JavaScript对象文字而不是JSON。 This is because JSON is also a valid JavaScript object/array literal when it is not contained in a string: 这是因为当JSON不包含在字符串中时,它也是有效的JavaScript对象/数组文字:

var obj = {"label.name.first":"foo","label.name.second":"bar"};

If you were to use it as a string, then it is JSON: 如果将其用作字符串,则为JSON:

var json = '{"label.name.first":"foo","label.name.second":"bar"}';
// json is a string, so it's JSON
var obj = JSON.parse(json); // parse the JSON into an object

The confusion is quote common because the JSON format is very similar to the format of JavaScript object and array literals. 这种混淆很常见,因为JSON格式与JavaScript对象和数组文字的格式非常相似。

Do this: 做这个:

json["label.name.first"]

However, I think you are misunderstanding the . 但是,我认为您误会了. notation. 符号。

And BTW your json isn't a JSON, it is a javascript object and not its notation. 顺便说一句,您的json不是JSON,它是一个javascript对象,而不是其符号。

It's json["label.name.first"] that would get you "foo" . json["label.name.first"]会让您"foo" Since your property's name contains characters that cannot be used in variable names. 由于您的媒体资源名称包含不能在变量名称中使用的字符。

If you're expecting to access these properties using the syntax json.label.name.first then your JSON needs to be: 如果希望使用语法json.label.name.first访问这些属性,那么您的JSON必须为:

var json = {
    "label":{
         "name":{
             "first":"foo",
             "second":"bar"
         }
    }
}

This is not right way to create object, you should create one like this. 这不是创建对象的正确方法,您应该创建一个这样的对象。

var json={"label":{"name":{"first":"foo","second":"bar"}}};  

it will also work as json string 它也可以作为json字符串

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

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