简体   繁体   English

获取返回的 JSON 字符串 C# 的值

[英]Grabbing the value of a returned JSON string C#

I have JSON reply and i need to grab "Id" from it.我有 JSON 回复,我需要从中获取“Id”。 I attempted 2 variations of the code below我尝试了以下代码的 2 个变体

    using (JsonDocument document = JsonDocument.Parse(jsonstring))
    {

         JsonElement root = document.RootElement;
         JsonElement resultsElement = root.GetProperty("Result");
         List<string> names = new List<string>();

         foreach (var result in resultsElement.EnumerateObject())
         {


               if (result.Value.TryGetProperty("Id", out resultsElement))
               {
                    names.Add(resultsElement.GetString());
               }
         }
   }

The requested operation requires an element of type 'Object', but the target element has type 'Number'.请求的操作需要类型为“Object”的元素,但目标元素的类型为“Number”。

adjusted EnumerateObject to Enumerate Array but i still get the same error with 'Array' - 'Object' instead of 'Object' - 'Array'将 EnumerateObject 调整为 Enumerate Array 但我仍然遇到相同的错误,使用 'Array' - 'Object' 而不是 'Object' - 'Array'

the JSON reply has this format: JSON 回复具有以下格式:

    {
    "code":1,
    "result":{
        "Id":1,
        "Name":"name"
        }
    }

I can't seem to be able to grab the specific Id using the bove method.我似乎无法使用 bove 方法获取特定的 Id。

I think you're making this hard for yourself;我认为你在为自己做这件事; it is much easier just to map to a type:映射到类型容易得多

public class MyRoot {
    [JsonProperty("code")]
    public int Code {get;set;}
    [JsonProperty("result")]
    public MyResult Result {get;set;}
}
public class MyResult {
    public int Id {get;set;}
    public string Name {get;set;}
}

and use:并使用:

var root = JsonConvert.DeserializeObject<MyRoot>(json);
var result = root.Result;
// etc

You can enumerate the parsed JsonDocument Root element and result element.您可以枚举已解析的JsonDocument Root元素和result元素。 Or do it recursively for every child node that has JsonValueKind.Object type and get the id value或者对每个具有JsonValueKind.Object类型的子节点递归执行并获取id

using (var document = JsonDocument.Parse(File.ReadAllText("test.json")))
{
    JsonElement root = document.RootElement;
    var names = new List<string>();
    Enumerate(root);

    void Enumerate(JsonElement element)
    {
        if (element.ValueKind == JsonValueKind.Object)
        {
            foreach (var item in element.EnumerateObject())
            {
                if (item.Value.ValueKind == JsonValueKind.Object)
                {
                    Enumerate(item.Value);
                }

                if (item.Value.ValueKind == JsonValueKind.Number && item.Name == "Id")
                {
                    //Console.WriteLine(item.Value.GetRawText());
                    names.Add(item.Value.GetRawText());
                }
            }
        }
    }
}

This code allows you to enumerate JSON with any number of nesting levels此代码允许您枚举具有任意数量嵌套级别的 JSON

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

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