简体   繁体   English

嵌套json数组解析

[英]nested json array parsing

I am having a difficult time parsing a json response with C# asp.net. 我很难用C#asp.net解析json响应。 mostly with the array within array structure of this response. 大多与该响应的数组结构内的数组有关。 I have edited the post to reflect the json object. 我已经编辑了帖子以反映json对象。 I think we can omit the deserialzation code. 我认为我们可以省略反序列化代码。

{"Level1":
 [
 { 
   A:"some",
   B:"more",
   C:"stuff"
 }
 ],"DataLevel":
 [[
     { "AnotherLevel":
        { 
          "File":"data" 
        }, 
       "More":"stuff"
     }
  ]]} 

  // C# code
  public class JsonObject
  {
      public Level1[] level1 {get;set;}
      public DataLevel[] datalevel {get;set;}
  }
  public class Level1
  {
      public string A {get;set;}
      public string B {get;set;}
      public string C {get;set;}
  }
  public class DataLevel
  { 
      // ??
      // Seems like this should be public AnotherLevel anotherlevel {get;set;}
      public string More {get;set;}
  }

Ok so looking at your data I would say your class definitions do not match the json you posted. 好的,所以查看您的数据,我会说您的类定义与您发布的json不匹配。 Look closely at it. 仔细看看。 You have an object with 2 properties. 您有一个具有2个属性的对象。 One is an array of objects, the other is an array of object arrays. 一个是对象数组,另一个是对象数组数组。 Below I have a different set of class definitions that should solve your problems. 下面有一组不同的类定义,它们可以解决您的问题。

public class OuterObject
{
     public FirstArrayObject[];
     public List<ObjInNestedArray[]>;
}

public class FirstArrayObject
{
   public string A;
   public string B;
   public string C;
}

public class ObjInNestedArray
{
     string property1;
     AnotherLevel AnotherLevel;

}

public class AnotherLevelObj
{
      string prop1;
}

OuterObject response = JsonConvert.DeserializeObject<OuterObject>(responseBodyAsString);

I don't know how good this is and didn't check if it is actually correct but you could try this website http://json2csharp.com/ , but it might help you in some way! 我不知道这有多好,也没有检查它是否真的正确,但是您可以尝试使用该网站http://json2csharp.com/ ,但是它可能以某种方式对您有所帮助!

This is the result I got when I used the json data you provided: 这是我使用您提供的json数据时得到的结果:

class Level1
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
}

public class RootObject
{
    public List<Level1> Level1 { get; set; }
    public List<List<>> DataLevel { get; set; }
}

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

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