如何通过超级嵌套Json的键获取值 [英] How to get the value by a key from a super nested Json

查看:44
本文介绍了如何通过超级嵌套Json的键获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"_embedded": {
"cscaia:status_report": {
  "_links": {
    "self": {
      "title": "status_report",
      "name": "status_report",
      "href": "https://api.dxc-dev-aia.hub-1.dev.us.insurance.dxc.com/quotes/ID-mrMxY1Dg/status_report"
    },
    "type": {
      "href": "https://diaas-dev.gtaia-test-domain.net/std-dev-lux-13100/insurance/schemas/quotes/statusReportDocument"
    },
    "up": {
      "href": "https://api.dxc-dev-aia.hub-1.dev.us.insurance.dxc.com/quotes/ID-mrMxY1Dg"
    }
  },
  "consistent": false,
  "messages": [
    {
      "message": "Incomplete attribute",
      "context": [
        {
          "propertyNames": [
            "quote$distributor_id"
          ]
        }
      ],
      "severity": "error",
      "code": "incomplete_attr"
    }
  ]
}

}

在这里,我需要找出键严重性"的值,但我不知道它的级别.我也没有嵌套它的父级的键,因为它总是嵌套在json中充满活力.

Here I need to find out the value of the key "severity" but i don't know the level of it .I don't have the key for its parent as well inside which it is nested as the json will always be dynamic.

我尝试使用扩展方法.但是对于嵌套键,我没有得到结果.

I have tried with an extension method. But for nested key i am not getting the result.

public static TType JsonValue<TType>(this JObject obj, string key)
    {
        object result = null; //default to null if nothing is found

        foreach (var item in obj)
        {
            var token = item;

            if (token.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase))
            {
                result = token.Value.ToObject<TType>(); //return the value found
                break;
            }

            if (!obj[token.Key].Children().Any())
                continue;

            var jt = obj[token.Key].ToString();

            if (!jt.StartsWith("["))
            {
                result = JsonValue<TType>(JObject.Parse(jt), key);
            }
            else
            {
                obj[token.Key].Children().ToList().ForEach(x =>
                {
                    //only the first match will be returned
                    result = JsonValue<TType>(JObject.Parse(x.ToString()), key);
                });
            }

            if (result != null)
                break;
        }
        return (TType)result;
    }

推荐答案

您可以采用递归方法,并在找到值后返回.

You could take a recursive approach and return if a value is found.

function getValue(object, key) {
    var value;

    if (!object || typeof object !== 'object') return;
    if (key in object) return object[key];

    Object.values(object).some(v => {
        value = getValue(v, key)
        return value !== undefined;
    });    

    return value;
}

var data = { _embedded: { "cscaia:status_report": { _links: { self: { title: "status_report", name: "status_report", href: "https://api.dxc-dev-aia.hub-1.dev.us.insurance.dxc.com/quotes/ID-mrMxY1Dg/status_report" }, type: { href: "https://diaas-dev.gtaia-test-domain.net/std-dev-lux-13100/insurance/schemas/quotes/statusReportDocument" }, up: { href: "https://api.dxc-dev-aia.hub-1.dev.us.insurance.dxc.com/quotes/ID-mrMxY1Dg" } }, consistent: false, messages: [{ message: "Incomplete attribute", context: [{ propertyNames: ["quote$distributor_id"] }], severity: "error", code: "incomplete_attr" }] } } };

console.log(getValue(data, 'severity'));
console.log(getValue(data, 'href'));

这篇关于如何通过超级嵌套Json的键获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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