简体   繁体   中英

Deserialise Json Array Windows Phone 8.0

my JsonArray is

   [
  {
    "A": "asdasd",
    "B": "asdasd",
    "C": "43543543",
    "D": "fdgdfgt54654",
    "E": "54tg54g54g"
  },
  {
    "A": "45tg54tg54g",
    "B": "g45erg45g",
    "C": "rhtfg4hg4g",
    "D": "hdfhg45yg",
    "E": "fgh45yg45"
  },
  {
    "A": "trh4yh45yg",
    "B": "gy45g4554egt5",
    "C": "54hg4rg45g",
    "D": "gtrg45g",
    "E": "fdg54g45g545454"
  }

]

And My Class is

public class RootObject
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
    public string D { get; set; }
    public string E { get; set; }
}

I am not able to deserialise this json array into RootObject Class...so please help...I have tries by accessing the individual members of array but that wasw very long method...so please help..

Your class should be:

public class RootObject
{
    public string details { get; set; }
    public string deal { get; set; }
    public string company_name { get; set; }
    public string validity { get; set; }
    public string coupon { get; set; }
}

Deserialize json string:

JsonConvert.DeserializeObject<List<RootObject>>(json string);

Your json object contains a list of the object you want to deserialize.

Basically you need to tell the deserializer that you have a List of your root objects, ie List<RootObject> then once you deserialize to this object you can iterate over each of the items in it.

Something like this works:

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization; // Contains JavaScriptSerializer from System.Web.Extensions

namespace Tester
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            deserialize();
        }

        public static void deserialize()
        {

            string jsonRaw = @"[
            {
                'A': 'asdasd',
                'B': 'asdasd',
                'C': '43543543',
                'D': 'fdgdfgt54654',
                'E': '54tg54g54g'
            },
            {
                'A': '45tg54tg54g',
                'B': 'g45erg45g',
                'C': 'rhtfg4hg4g',
                'D': 'hdfhg45yg',
                'E': 'fgh45yg45'
            },
            {
                'A': 'trh4yh45yg',
                'B': 'gy45g4554egt5',
                'C': '54hg4rg45g',
                'D': 'gtrg45g',
                'E': 'fdg54g45g545454'
            }
        ]";

            List<RootObject> rObjects = new JavaScriptSerializer().Deserialize<List<RootObject>>(jsonRaw);
            foreach (var o in rObjects)
            {
                Console.WriteLine(o.A + ", " + o.B + ", " + o.C + ", " + o.D + ", " + o.E);
            }
        }
    }

    public class RootObject
    {
        public string A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
        public string D { get; set; }
        public string E { get; set; }
    }
}

Above code outputs:

asdasd, asdasd, 43543543, fdgdfgt54654, 54tg54g54g
45tg54tg54g, g45erg45g, rhtfg4hg4g, hdfhg45yg, fgh45yg45
trh4yh45yg, gy45g4554egt5, 54hg4rg45g, gtrg45g, fdg54g45g545454
var syncEntitiesArray = [{"EntityName":"Bulletins","LastSyncDate":"20150525_072418"},{"EntityName":"DatasetFilters","LastSyncDate":"20150525_072418"},{"EntityName":"Datasets","LastSyncDate":"20150525_072418"},{"EntityName":"DatasetItems","LastSyncDate":"20150525_072418"}]

public class DateModel
{
    public string EntityName { get; set; }
    public string LastSyncDate { get; set; }
}

IList<DateModel> myObject = JsonConvert.DeserializeObject<IList<DateModel>>(syncEntitiesArray);

foreach (var obj in myObject)
  {
       string nodeName = obj.EntityName;
       string nodeValue = obj.LastSyncDate;
  }

This works perfect for me. Hope this helps you as well. Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM