简体   繁体   English

遍历JSON以获取孩子的名字

[英]Traversing JSON to get name of child

I want to get the name of child under Version1 and Version 2. Examples below for name of child are "Customer" and "Invoice". 我想在版本1和版本2下获得子名称。下面的子名称示例为“客户”和“发票”。

How would I get it using javascript? 我如何使用javascript来获得它?

I tried Errors.Version1.[0] 我尝试了Errors.Version1。[0] to get name but that does not seem to work. 获得名称,但这似乎行不通。 I dont want to use Errors.Version1.Customer because it can change. 我不想使用Errors.Version1.Customer,因为它可以更改。 It can be "Customer" or "Invoice". 它可以是“客户”或“发票”。 I just want to get whatever name it is. 我只是想知道它的名字。

Thanks! 谢谢!

JSON example JSON范例

{
  "Errors": [
    {
      "Version1": {
        "Customer": {
          "BillAddr": {
            "Id": "Id1",
            "Line1": "Line11",
            "Line2": "Line21",
            "Line3": "Line31",
            "Line4": "Line41",
            "Line5": "Line51",
            "City": "City1",

          }
        }
      },
      "Version2": {
        "Invoice": {
          "BillAddr": {
            "Id": "Id1",
            "Line1": "Line11",
            "Line2": "Line21",
            "Line3": "Line31",
            "Line4": "Line41",
            "Line5": "Line51",
            "City": "City1",

          }
        }
      }
    }
  ]
}

The correct notation would be 正确的符号是

Errors[0].Version1[0] // however still won't work

Because you have an extra array wrapper in your json and you mixed notations. 因为您的json中有一个额外的数组包装器,并且混合了符号。
However, object properties can't be accessed by their index, because their order is not guaranteed. 但是,对象属性无法通过其索引访问,因为不能保证其顺序。 So you either need to convert Version1 and Version2 to an array, or have to access the properties by name. 因此,您需要将Version1Version2转换为数组,或者必须按名称访问属性。

Errors[0].Version1["Customer"] //works for your current markup

Also, use either dot-Notation object.property or Bracket-Notation object["property"] - you can't mix it. 另外,请使用dot-Notation object.property Bracket-Notation object["property"] -您无法混合使用。

The following would work for your Notation Errors.Version1[0] : 以下将适用于您的Notation Errors.Version1[0]

{
  "Errors":
    {
      "Version1": [
        {
          "BillAddr": {
            "Id": "Id1",
            "Line1": "Line11",
            "Line2": "Line21",
            "Line3": "Line31",
            "Line4": "Line41",
            "Line5": "Line51",
            "City": "City1",

          }
        }
      ]
    }
}

As an array the order is guaranteed to be fixed, but you lose the information customer because arrays have no keys but only indices. 作为数组,可以保证顺序是固定的,但是您会失去信息客户,因为数组没有键,只有索引。

You can't reference object properties as indexes; 您不能将对象属性引用为索引; in other words you can't do: 换句话说,您不能执行以下操作:

var foo = {a:1};
foo[0]; // Doesn't work

What you can do though is reference the property using a similar syntax as an index: 但是,您可以做的是使用类似的语法作为索引来引用属性:

var foo = {a:1};
foo[someBoolean ? 'Customer' : 'Invoice']; // Does work

But, if you just want to get ANY properties of an object, regardless of what they're named? 但是,如果您只想获取对象的任何属性,无论它们的名称是什么? Well, then you need to iterate through them: 好吧,那么您需要遍历它们:

var foo = {randomName:1};
for (var key in foo) {
    // key=="randomName"
    // foo[key]==foo["randomName"]==foo.randomName==1
    // do whatever you want with foo[key];
}

However, I'd recommend using a library (both Underscore or jQuery have their own "each" functions) rather than the native JS, because if you use any libraries that add custom properties they could mess things up. 但是,我建议您使用一个库(Underscore或jQuery都有其各自的“每个”函数)而不是本机JS,因为如果您使用任何添加自定义属性的库,它们可能会使事情搞砸。

If it can only ever be one or the other, then just do a simple test: 如果只能是一个,则可以做一个简单的测试:

var type = ("Customer" in Errors[0].Version1) ? "Customer" : "Invoice",
    obj  = Errors[0].Version1[type];

If it's more dynamic than just those two possible results, then you need to do a for ... in iteration through the keys of the object. 如果它比这两个可能的结果更具动态性,那么您需要通过对象的键进行for ... in迭代。

That said, if you have 100% ownership of this data format, and the ability to change it, might I suggest removing one level, and instead giving the error an error_type property, rather than trying to guess it? 就是说,如果您对此数据格式拥有100%的所有权,并且能够更改它,我是否建议您删除一个级别,而是为错误提供error_type属性,而不是尝试猜测它?

{
    Errors : [{
        Version1 : {
            ErrorType : "Customer",
            BillAddr : {
                Id    : "Id1",
                Line1 : "Line11",
                Line2 : "Line21",
                Line3 : "Line31",
                Line4 : "Line41",
                Line5 : "Line51",
                City  : "City1"
            }
        },

        Version2 : { /* ... */ }
    }]
}

Alternatively, you could make VersionX an array, rather than an object, and to that end, you could have multiple errors, held as objects (with their own error-type properties), at which point, figuring out what you're dealing with, through: 另外,您可以将VersionX为数组而不是对象,为此,您可能会有多个错误,这些错误被保存为对象(具有其自己的error-type属性),此时,您需要弄清楚要处理的内容,通过:

Errors[0].Version1[0].ErrorType;

becomes dead-simple. 变得简单。

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

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