在节点具有数值的情况下解析JSON [英] Parse JSON where node has numeric value

查看:111
本文介绍了在节点具有数值的情况下解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解析以下JSON以获取TestCycleName的值.挑战在于确定根节点,因为它以数字开头.

How do I parse the following JSON to get the value of TestCycleName. The challenge is in identifying the root node since it starts with a number.

我的实现将使用JSON.net在C#中实现

My implementation will be in C# using JSON.net

{
  "URL": "rest/zapi/latest/cycle?projectId=##projectId##&versionId=##versionId##",
  "Method": "GET",
  "Parameters": {
    "1": {
      "VersionName": "Custom Pipes Development",
      "TestCycleName": "SetMaxFutureDateFromCustomerField_Mobile"
    },
    "2": {
      "VersionName": "Recurring payments 1.5",
      "TestCycleName": "Internet Full Regression Pack - Mobile"
    },
    "3": {
      "VersionName": "Customer Profile Phase 1.5",
      "TestCycleName": "Customer Profile Regression Pack - Desktop"
    },
    "4": {
      "VersionName": "Customer Profile Phase 1.5",
      "TestCycleName": "Customer Profile E2E Pack - Desktop"
    },
    "5": {
      "VersionName": "Customer Profile Phase 1.5",
      "TestCycleName": "Customer Profile Regression Pack - Mobile"
    },
    "6": {
      "VersionName": "Internet Phase 1.2",
      "TestCycleName": "Internet API Regression Pack"
    },
    "7": {
      "VersionName": "Internet Phase 1.2",
      "TestCycleName": "Internet GUI Regression Pack - Desktop"
    },
    "8": {
      "VersionName": "Internet Phase 1.2",
      "TestCycleName": "Internet GUI Regression Pack - Mobile"
    },
    "9": {
      "VersionName": "Internet Phase 1.2",
      "TestCycleName": "Regression Library Admin Tool - E2E Tests"
    },
    "10": {
      "VersionName": "Internet Phase 1.2",
      "TestCycleName": "Regression Library E2E Tests - Mobile"
    },
    "11": {
      "VersionName": "Recurring payments 1.5",
      "TestCycleName": "[Internet] Autopay API Automation Regression Pack"
    }
  }
}

推荐答案

尝试如下代码:

using Newtonsoft.Json.Linq;
using System;
using System.Linq;


namespace ConsoleApp1
{
  class Program
  {
    static void Main(string[] args)
    {
        string jsonString = "{\"URL\": \"rest/zapi/latest/cycle?projectId=##projectId##&versionId=##versionId##\",\"Method\": \"GET\",\"Parameters\": {\"1\": {\"VersionName\": \"Custom Pipes Development\",\"TestCycleName\": \"SetMaxFutureDateFromCustomerField_Mobile\"},\2"\": {\"VersionName\": \"Recurring payments 1.5\",\"TestCycleName\": \"Internet Full Regression Pack - Mobile\"}}}";

        var parameters = JObject.Parse(jsonString)["Parameters"];

        foreach (var item in parameters.OfType<JProperty>())
        {
            var innerObject = JObject.Parse(item.Value.ToString())["TestCycleName"];

            Console.WriteLine(innerObject.ToString());
        }

        Console.ReadLine(); 
    }
  }
}

这篇关于在节点具有数值的情况下解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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