簡體   English   中英

嵌套json數組解析

[英]nested json array parsing

我很難用C#asp.net解析json響應。 大多與該響應的數組結構內的數組有關。 我已經編輯了帖子以反映json對象。 我認為我們可以省略反序列化代碼。

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

好的,所以查看您的數據,我會說您的類定義與您發布的json不匹配。 仔細看看。 您有一個具有2個屬性的對象。 一個是對象數組,另一個是對象數組數組。 下面有一組不同的類定義,它們可以解決您的問題。

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);

我不知道這有多好,也沒有檢查它是否真的正確,但是您可以嘗試使用該網站http://json2csharp.com/ ,但是它可能以某種方式對您有所幫助!

這是我使用您提供的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