-3

你好guyzzzz我想同時存儲在衆數組key和value ...以下是我的JSON ..窗口電話:deserialise並同時存儲JSON鍵和值模態

{ 
    "Example": [ 
    { 
     "Invoice": { 
     "TFDGFF": " 200", 
     "BF": " 200", 
     "MD": "10", 
     "EFM": " 12", 
     "ATT": "4" 
     }, 
     "TF": "200" 
    }, 
    { 
     "Invoice": { 
     "DF": " 49", 
     "DR": " 49", 
     "KJ": "4", 
     "LKIH": " 14", 
     "KJGU": "4" 
     }, 
     "DF": "49" 
    } 

請告訴我如何反序列化這個JSON,這樣我就可以在模態類中存儲鍵和值,應該如何設計模態類?

回答

0
public class Invoice 
{ 
    public string TF { get; set; } 
    public string BF { get; set; } 
    public string MD { get; set; } 
    public string EFM { get; set; } 
    public string ATT { get; set; } 
    public string FPK { get; set; } 
} 

public class Example 
{ 
    public Invoice Invoice { get; set; } 
    public string TF { get; set; } 
} 

public class RootObject 
{ 
    public List<Example> Example { get; set; } 
} 

利用json2csharp.com

現在利用newtonsoft包來反序列化JSON

var obj = JsonConvert.DeserializeObject<RootObject>(yourstring); 

對於新JSON

public class RootObject 
     { 
      public List<Example> Example { get; set; } 
     } 

     public class Example 
     { 
      public Dictionary<string, string> Invoice { get; set; }   
     } 

而你把它用

string [email protected]" { ""Example"": [ { ""Invoice"": { ""TFDGFF"": ""200"", ""BF"": ""200"", ""MD"": ""10"", ""EFM"": ""12"", ""ATT"": ""4"" }, ""TF"": ""200"" }, { ""Invoice"": { ""DF"": "" 49"", ""DR"": "" 49"", ""KJ"": ""4"", ""LKIH"": "" 14"", ""KJGU"": ""4"" }, ""DF"": ""49"" }]}"; 
      var obj = JsonConvert.DeserializeObject<RootObject>(temp); 

對於新問題關於綁定

for (int i = 0; i < obj.Example.Count(); i++) 
       { 
        foreach (KeyValuePair<string, string> pair in obj.Example[i].Invoice) 
        { 
         string temp3 = pair.Key; 
         string temp2 = pair.Value; 
        } 
       } 
+0

@jenin:我知道這already..this不儲存JSON的鑰匙......我有我的json..so動態密鑰我還需要在模式中存儲密鑰 – Shanky

+0

那麼你提供的json沒有任何動態數據,你可以使用Newtonsoft自動解串器而不是你的自定義類或最好的方法是製作一個Dictonary對象並利用它鍵值對 – Jerin

+0

@jenin:由於我的JSON數據包含多個「發票」鍵..如何將其存儲在字典? 你能舉幾個例子嗎? – Shanky