深度嵌套的JSON标签值

时间:2020-06-11 21:55:49

标签: c# json

我有一个嵌套很深的JSON字符串-像这样:

{
    "decisionElements": [
        {
            "serviceName": "PreciseId",
            "applicantId": "APPLICANT_CONTACT_ID_1",
            "decision": "R12",
            "score": 123,
            "decisionText": "Refer",
            "appReference": "2323213",
            "rules": [],
            "otherData": {
                "json": {
                    "fraudSolutions": {
                        "response": {
                            "products": {
                                "preciseIDServer": {
                                    "sessionID": "2232GTH",
                                    "header": {},
                                    "messages": {},
                                    "summary": {},
                                    "preciseMatch": {},
                                    "kba": {
                                        "general": {
                                            "sessionID": "43124324",
                                            "numberOfQuestions": 4,
                                            "kbaresultCodeDescription": "processing successful questions returned",
                                            "kbaresultCode": 0
                                        },
                                        "questionSet": [
                                            {
                                                "questionType": 3,
                                                "questionText": "According to your credit profile, you may have opened an auto loan in or around June 1997. Please select the lender for this account. If you do not have such an auto loan, select 'NONE OF THE ABOVE/DOES NOT APPLY'.",
                                                "questionSelect": {
                                                    "questionChoice": [
                                                        "abc back",
                                                        "AUTO LEASE",
                                                        "FINANCE",
                                                        "BANK",
                                                        "NONE OF THE ABOVE/DOES NOT APPLY"
                                                    ]
                                                }
                                            }
                                        ]
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    ]
}

我正在尝试检索sessionID的值。我尝试根据较早的stackoverflow帖子执行此操作,但是sessionID为null。以下是我尝试检索值的方法:

var data = (JObject)JsonConvert.DeserializeObject(JSONResponse);
data.SelectToken("clientResponsePayload.decisionElements[0].otherData[0].json[0].fraudSolutions[0].response[0].products[0].preciseIDServer[0].sessionID")?.Value<string>()

我们将不胜感激任何帮助。

2 个答案:

答案 0 :(得分:1)

针对问题中发布的 Json
1- Json
中没有clientResponsePayload 2-对于 SessionId 路径,您有一个数组decisionElements,其他标签只是 objects

以下代码填充了SessionId

var data = (JObject)JsonConvert.DeserializeObject(JSONResponse);
string seesionId = data.SelectToken("decisionElements[0].otherData.json.fraudSolutions.response.products.preciseIDServer.sessionID")?.Value<string>();

路径:

decisionElements[0].otherData.json.fraudSolutions.response.products.preciseIDServer.sessionID

希望您对此有帮助。

答案 1 :(得分:1)

这里是通过将对象映射到json的尝试。 json键必须与类对象具有相同的名称,才能使用此解决方案,因此我已附加了我在Visual Studio中使用的称为粘贴json作为类的功能所使用的类。我已经附上了输出的图像 enter image description here

var json = @"{'decisionElements': [
                {
                        'serviceName': 'PreciseId',
                    'applicantId': 'APPLICANT_CONTACT_ID_1',
                    'decision': 'R12',
                    'score': 123,
                    'decisionText': 'Refer',
                    'appReference': '2323213',
                    'rules': [],
                    'otherData': {
                        'json': {
                            'fraudSolutions': {
                                'response': {
                                    'products': {
                                        'preciseIDServer': {
                                            'sessionID': '2232GTH',
                                            'header': {},
                                            'messages': {},
                                            'summary': {},
                                            'preciseMatch': {},
                                            'kba': {
                                                'general': {
                                                    'sessionID': '43124324',
                                                    'numberOfQuestions': 4,
                                                    'kbaresultCodeDescription': 'processing successful questions returned',
                                                    'kbaresultCode': 0
                                                },
                                                'questionSet': [
                                                    {
                                                        'questionType': 3,
                                                        'questionText': 'According to your credit profile, you may have opened an auto loan in or around June 1997. Please select the lender for this account. If you do not have such an auto loan, select NONE OF THE ABOVE/DOES NOT APPLY.',
                                                        'questionSelect': {
                                                            'questionChoice': [
                                                                'abc back',
                                                                'AUTO LEASE',
                                                                'FINANCE',
                                                                'BANK',
                                                                'NONE OF THE ABOVE/DOES NOT APPLY'
                                                            ]
            }
        }
                                                ]
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            ]
        }";
                    var obj = JsonConvert.DeserializeObject<Rootobject>(json);
                    Console.WriteLine(obj.decisionElements.Select(h=>h.otherData.json.fraudSolutions.response.products.preciseIDServer.sessionID).FirstOrDefault());

      public class Rootobject
        {
            public Decisionelement[] decisionElements { get; set; }
        }

        public class Decisionelement
        {
            public string serviceName { get; set; }
            public string applicantId { get; set; }
            public string decision { get; set; }
            public int score { get; set; }
            public string decisionText { get; set; }
            public string appReference { get; set; }
            public object[] rules { get; set; }
            public Otherdata otherData { get; set; }
        }

        public class Otherdata
        {
            public Json json { get; set; }
        }

        public class Json
        {
            public Fraudsolutions fraudSolutions { get; set; }
        }

        public class Fraudsolutions
        {
            public Response response { get; set; }
        }

        public class Response
        {
            public Products products { get; set; }
        }

        public class Products
        {
            public Preciseidserver preciseIDServer { get; set; }
        }

        public class Preciseidserver
        {
            public string sessionID { get; set; }
            public Header header { get; set; }
            public Messages messages { get; set; }
            public Summary summary { get; set; }
            public Precisematch preciseMatch { get; set; }
            public Kba kba { get; set; }
        }

        public class Header
        {
        }

        public class Messages
        {
        }

        public class Summary
        {
        }

        public class Precisematch
        {
        }

        public class Kba
        {
            public General general { get; set; }
            public Questionset[] questionSet { get; set; }
        }

        public class General
        {
            public string sessionID { get; set; }
            public int numberOfQuestions { get; set; }
            public string kbaresultCodeDescription { get; set; }
            public int kbaresultCode { get; set; }
        }

        public class Questionset
        {
            public int questionType { get; set; }
            public string questionText { get; set; }
            public Questionselect questionSelect { get; set; }
        }

        public class Questionselect
        {
            public string[] questionChoice { get; set; }
        }