windows phone: deserialise and store both json key and value to modal

115 views Asked by At

Hello guyzzzz i want to store both key and value in modal class...below is my 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"
    }

please tell me how to deserialise this json so that i can store both key as well as value in modal class and how should be the design of modal class ??

1

There are 1 answers

12
Jerin On BEST ANSWER
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; }
}

Make use of json2csharp.com

Now make use of newtonsoft package to deserialize your json

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

For New JSON

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

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

And you call it using

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 new Question Regarding Bind

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