简体   繁体   中英

Deserialize json string with multiple objects

I have the following JSON response after calling an API:

{
   "Item": "Some value",
   "Status": "ST",
   "Details": "something here...",
   "TargetObject": [
      {
         "Value1": "1",
         "Value2": [
            {
               "ABC1": "......",
               "ABC2": "...",
               "ABC3": "..."
            }
         ],
         "Value3": [
            {
               "ABCD1": "......",
               "ABCD2": "...",
               "ABCD3": [
                  {
                     "ABCVal": "123"
                  },
                  {
                     "Extra": "123"
                  }
               ]
            }
         ]
      }
   ]
}

What I want to do is to return the values from TargetObject but this key has more than one objects/arrays inside so my question is: how can I return only those values and print the following JSON result:

{
   "TargetObject": [
      { 
        "Value1": "1",
        "Value2": [
           {
              "ABC1": "......",
              "ABC2": "...",
              "ABC3": "..."
           }
        ],
        "Value3": [
           {
              "ABCD1": "......",
              "ABCD2": "...",
              "ABCD3": [
                 {
                    "ABCVal": "123"
                 },
                 {
                    "Extra": "123"
                 }
              ]
           }
        ]
     }
   ]
}

This is what I tried:

public async Task<List<List<MyModel>>> GetAsync(string url)
{
    /*
        Code to get the above JSON
    */
    
    //Trying to return the expected JSON
    return JsonConvert.DeserializeObject<List<List<MyModel>>>(jsonString);
}

And this is my Model:

public class MyModel
{
    public string TargetObject { get; set; }
    public string Value1 { get; set; }
    public string Value2 { get; set; }
    public string ABC1 { get; set; }
    public string ABC2 { get; set; }
    public string ABC3 { get; set; }
    public string Value3 { get; set; }
    public string ABCD1 { get; set; }
    public string ABCD2 { get; set; }
    public string ABCD3 { get; set; }
    public string ABCVal { get; set; }
    public string Extra { get; set; }
}

At the moment I'm getting the following error:

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[myProjectNamespace.Models.MyModel]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly.

What about dynamic object?

     public async Task<dynamic> GetAsync(string url)
     {
      /*
          Code to get the above JSON
      */     
      return Newtonsoft.Json.JsonConvert.DeserializeObject(res);
     }

PS: install Newtonsoft.Json

If you have strongly typed JSON then you need to create a few models (DTO) to represent a structure of the JSON. Note: ABCD3 has different types in Value2 and Value3 , you should define it by yourself according to your data

public class Root
{
    public List<TargetObject> TargetObject { get; set; } 
}
public class TargetObject
{
    public string Value1 { get; set; } 
    public List<Value2> Value2 { get; set; } 
    public List<Value3> Value3 { get; set; } 
}
public class Value2
{
    public string ABC1 { get; set; } 
    public string ABC2 { get; set; } 
    public string ABC3 { get; set; } 
}
public class Value3
{
    public string ABCD1 { get; set; } 
    public string ABCD2 { get; set; } 
    public List<ABCD3> ABCD3 { get; set; } 
}
public class ABCD3
{
    public string ABCVal { get; set; } 
    public string Extra { get; set; } 
}

then

 return JsonConvert.DeserializeObject<Root>(jsonString);

In case when you need just to get content of TargetObject as a string , use:

var jsonDoc = JsonDocument.Parse(jsonString);
var targetObject = jsonDoc.RootElement.GetProperty("TargetObject").GetRawText();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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