2017-06-23 80 views
-2

我有我需要從C#訪問的JSON數據。該章節元素看起來是這樣的:如何解析JSON到C#對象?

"chapters": [ 
    [ 
     2, 
     1416420134.0, 
     "2", 
     "546cdb2645b9efbff4582d51" 
    ], 
    [ 
     1, 
     1411055241.0, 
     null, 
     "541afe8945b9ef69885d3d74" 
    ], 
    [ 
     0, 
     1414210972.0, 
     "0", 
     "544b259c45b9efb061521235" 
    ] 
] 

這裏是打算包含這些數據我的C#類:

public class test 
{ 
    public string[] chapters { get; set; } 
} 

public class TChapter 
{ 
    public test[] aa { get; set; } 
} 

我如何解析JSON到C#對象?

+0

看看'Newtonsoft.JSON' – NtFreX

+0

[在C#中解析JSON]可能的副本(https://stackoverflow.com/questions/1212344/parse-json-in-c-sharp) – Darendal

+2

是什麼讓你覺得你的類代表你的json? – maccettura

回答

0

使用Newtonsoft JSON你會想要做類似下面的

using System; 
using Newtonsoft.Json; 

namespace JsonDeserializationTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var chaptersAsJson = "[" + 
           " [" + 
           "  2," + 
           "  1416420134.0," + 
           "  \"2\"," + 
           "  \"546cdb2645b9efbff4582d51\"" + 
           " ], " + 
           " [" + 
           "  1," + 
           "  1411055241.0," + 
           "  null," + 
           "  \"541afe8945b9ef69885d3d74\"" + 
           " ], " + 
           " [" + 
           "  0," + 
           "  1414210972.0," + 
           "  \"0\"," + 
           "  \"544b259c45b9efb061521235\"" + 
           " ]" + 
           "]"; 
      var chaptersAsTwoDObjectArray = JsonConvert.DeserializeObject<object[][]>(chaptersAsJson); 

      // Use the chapters array 
      foreach (object[] chapter in chaptersAsTwoDObjectArray) 
      { 
       // what do you want to do with the object array? 
       Console.WriteLine(String.Join(", ", chapter)); 
      } 

      Console.WriteLine("Finished."); 
     } 
    } 
} 

注意你的類不符合你的JSON排隊。

+0

http://www.mangaeden.com/api/manga/5372389645b9ef5a0b1d20d8/ –

+0

那麼你的章節元素是一個對象數組的數組?那些對象數組包含一個整數,一個小數,一個字符串和一個字符串?你設計了這個JSON嗎?它可以改變嗎?對象數組中的四個對象的用途是什麼? – jlavallet

+0

我編輯了代碼以反映您的JSON數據。 – jlavallet