Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解析节点具有数值的JSON_C#_Json_Parsing_Json.net - Fatal编程技术网

C# 解析节点具有数值的JSON

C# 解析节点具有数值的JSON,c#,json,parsing,json.net,C#,Json,Parsing,Json.net,如何解析以下JSON以获取TestCycleName的值。挑战在于识别根节点,因为它以数字开头 我的实现将是使用JSON.net的C语言 { "URL": "rest/zapi/latest/cycle?projectId=##projectId##&versionId=##versionId##", "Method": "GET", "Parameters": { "1": { "VersionName": "Custom Pipes Developme

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

我的实现将是使用JSON.net的C语言

{
  "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"
    }
  }
}

尝试这样的方法当然没有经过测试,但是您可以调试它:

// You get Parameters object
var parameters = JObject.Parse(jsonString)["Parameters"];

// parameters can further be accessed via key/value pair using JProperty
foreach( var item in parameters.OfType<JProperty>()){

  // each item has a key and a value i.e item.Name and item.Value, 
  // key = numeric in your data, value is a json object.
  // take object and further parse it to reach TestCycleName

  var innerObject = JObject.Parse(item.Value.ToString())["TestCycleName"];

  Console.WriteLine(innerObject.ToString());

}
我有一些类似的东西,请在这里查看:

参数是一个字典,因此您可以编写如下内容:

public class Rootobject
{
    public string URL { get; set; }
    public string Method { get; set; }
    public Dictionary<int, Parameter> Parameters { get; set; }
}

public class Parameter
{
    public string VersionName { get; set; }
    public string TestCycleName { get; set; }
}
然后将其反序列化:

var result = JsonConvert.DeserializeObject<Rootobject>(json_string);

//You can get your TestCycleNames for example in string array
string[] testCycleName = result.Parameters.Select(p => p.Value.TestCycleName).ToArray(); 
请尝试以下代码:

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字符串,然后转到Visual studio,编辑->粘贴特殊->将json粘贴为类可能的重复:如果可以,让制作JSON的人员对其进行更改,使其返回数组而不是单独对象的列表。请使用中建议的参数字典,否则获取此错误无法从“Newtonsoft.JSON.Linq.JToke”转换为项处的字符串。value@mach正如我所说,我在Stackoverflow中写的,没有测试它。更新了我的答案。需要将值转换为字符串,因为JObject.Parse需要解析字符串。