4

I have JSON reply and i need to grab "Id" from it. I attempted 2 variations of the code below

    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'.

adjusted EnumerateObject to Enumerate Array but i still get the same error with 'Array' - 'Object' instead of 'Object' - 'Array'

the JSON reply has this format:

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

I can't seem to be able to grab the specific Id using the bove method.

1
  • There's no array in this JSON string. It's an object with a result property that holds another object Commented Mar 11, 2020 at 13:02

2 Answers 2

8

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
Sign up to request clarification or add additional context in comments.

Comments

3

You can enumerate the parsed JsonDocument Root element and result element. Or do it recursively for every child node that has JsonValueKind.Object type and get the id value

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.