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;
}
}
@jenin:我知道這already..this不儲存JSON的鑰匙......我有我的json..so動態密鑰我還需要在模式中存儲密鑰 – Shanky
那麼你提供的json沒有任何動態數據,你可以使用Newtonsoft自動解串器而不是你的自定義類或最好的方法是製作一個Dictonary對象並利用它鍵值對 – Jerin
@jenin:由於我的JSON數據包含多個「發票」鍵..如何將其存儲在字典? 你能舉幾個例子嗎? – Shanky