JSON 键是否需要唯一? [英] Do JSON keys need to be unique?

查看:37
本文介绍了JSON 键是否需要唯一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题与我之前提出的一个问题有关:帮助解析简单的 JSON(在 JAVA ME 中使用 JSON)

The following question is related to a question that I had asked earlier: Help parsing simple JSON (using JSON for JAVA ME)

JSON 键是否需要唯一?例如,我在解析以下 XML(使用 JSON ME)时遇到问题:

Do JSON keys need to be unique? For example, I was having trouble parsing the following XML (with JSON ME):

{
  "name" : "JACK",
  "name" : "JILL",
  "name" : "JOHN",
  "name" : "JENNY",
  "name" : "JAMES",
  "name" : "JIM"
}

而且,显然,这是因为键必须是唯一的.我只是想知道这是否在所有情况下都是正确的.例如,如果我使用的不是 JSON ME,我是否能够解析所有这些名称?

And, apparently, its because the keys must be unique. I'm just wondering if thats true in all cases or not. For example, if I were using something other than JSON ME, would I be able to parse all of these names?

谢谢.

推荐答案

使用多个同名键不会出现错误",但在 JSON 中,最后一个同名键是将被使用.

There is no "error" if you use more than one key with the same name, but in JSON, the last key with the same name is the one that is going to be used.

在您的情况下,键名称"最好包含一个数组作为其值,而不是包含多个键名称".同一个对象或事物"有两个名称,或者两个相同的属性发生冲突,这没有多大意义.

In your case, the key "name" would be better to contain an array as it's value, instead of having a number of keys "name". It doesn't make much sense the same object or "thing" to have two names, or two of the same properties that are in conflict.

例如:

{
  "name" : [ "JOHN", "JACK", "...", ... ]
}

这篇关于JSON 键是否需要唯一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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