windows phone:反序列化并将json键和值存储到模态

时间:2015-06-16 06:33:30

标签: c# windows-phone-8 windows-7 windows-phone-8.1 json-deserialization

你好guyzzzz我想在模态类中存储键和值...下面是我的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进行反序列化,以便我可以在模态类中存储键和值以及如何设置模态类?

1 个答案:

答案 0 :(得分: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 temp=@" { ""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;
                    }
                }