简体   繁体   中英

Deserializing json using asp.net libraries

I'm currently trying to deserialize the following JSON output in silverlight without javascript deserializer. I heard there's a way to do this using JsonArray and LINQ, but I can't quite figure it out.

{
    "Security": {
        "CIK": "0000789019",
        "Cusip": "594918104",
        "Symbol": "MSFT",
        "ISIN": "US5949181045",
        "Valoren": "951692",
        "Name": "Microsoft Corporation",
        "Market": "NASDAQGS",
        "CategoryOrIndustry": "TECHNOLOGY",
        "Outcome": "Success",
        "Message": null,
        "Identity": null,
        "Delay": 0
    },
    "StartDate": "7/1/2011",
    "EndDate": "7/6/2011",
    "Quotes": [{
        "Date": "7/5/2011",
        "Last": 26.03,
        "Open": 26.1,
        "LastClose": 26.02,
        "High": 26.15,
        "Low": 25.9,
        "ChangeFromOpen": -0.07,
        "PercentChangeFromOpen": -0.268,
        "ChangeFromLastClose": 0.01,
        "PercentChangeFromLastClose": 0.038,
        "Volume": 37803000,
        "SplitRatio": 1,
        "LastAdjusted": 26.03,
        "OpenAdjusted": 26.1,
        "LastCloseAdjusted": 26.02,
        "HighAdjusted": 26.15,
        "LowAdjusted": 25.9,
        "ChangeFromOpenAdjusted": -0.07,
        "ChangeFromLastCloseAdjusted": 0.01,
        "VolumeAdjusted": 37803000,
        "NotTraded": false,
        "Outcome": "Success",
        "Message": null,
        "Identity": null,
        "Delay": 0
    }, {
        "Date": "7/1/2011",
        "Last": 26.02,
        "Open": 25.93,
        "LastClose": 26,
        "High": 26.17,
        "Low": 25.84,
        "ChangeFromOpen": 0.09,
        "PercentChangeFromOpen": 0.347,
        "ChangeFromLastClose": 0.02,
        "PercentChangeFromLastClose": 0.077,
        "Volume": 52914500,
        "SplitRatio": 1,
        "LastAdjusted": 26.02,
        "OpenAdjusted": 25.93,
        "LastCloseAdjusted": 26,
        "HighAdjusted": 26.17,
        "LowAdjusted": 25.84,
        "ChangeFromOpenAdjusted": 0.09,
        "ChangeFromLastCloseAdjusted": 0.02,
        "VolumeAdjusted": 52914500,
        "NotTraded": false,
        "Outcome": "Success",
        "Message": null,
        "Identity": null,
        "Delay": 0
    }],
    "Outcome": "Success",
    "Message": null,
    "Identity": "Cookie",
    "Delay": 0.014001
}

What would be the best approach to doing something like this where I'm trying to extract the Date and LastAdjusted from the Quotes array and put each into their own arrays?

Here's how far I got to get the json into a stream:

downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
            downloader.OpenReadAsync(serviceUri);
        }

        void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Stream responseStream = e.Result;
                //add code here
            }
        }

The following code for Silverlight will take a JSON string, deserialize it to a "Listing" object and then loop through a list of quotes for the listing to extract the Date and LastAdjusted. Make sure to add a reference to System.ServiceModel.Web to your project in order to access the DataContractJsonSerializer (MSDN) class.

C#

string json = // Your JSON string
Listing myListing = DeserializeJSON(json);
foreach (Quote quote in listing.Quotes)
{
    DateTime dt = quote.Date;
    Double lastAdjusted = quote.LastAdjusted;
}

public Listing DeserializeJSON(string json)
{
    using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
    {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Listing));
        return (Listing)serializer.ReadObject(ms);
    }
}

Classes

    public class Listing
    {
        public Security Security { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public List<Quote> Quotes { get; set; }
    }

    public class Security
    {
        public string CIK {get; set;}
        public string Cusip {get; set;}
        public string Symbol {get; set;}
        public string ISIN {get; set;}
        public string Valoren {get; set;}
        public string Name {get; set;}
        public string Market {get; set;}
        public string CategoryOrIndustry {get; set;}
        public string Outcome {get; set;}
        public string Message {get; set;}
        public string Identity {get; set;}
        public string Delay { get; set; }
    }

    public class Quote
    {
        public DateTime Date { get; set; }
        public Double Last { get; set; }
        public Double Open { get; set; }
        public Double LastClose { get; set; }
        public Double High { get; set; }
        public Double Low { get; set; }
        public Double ChangeFromOpen { get; set; }
        public Double PercentChangeFromOpen { get; set; }
        public Double ChangeFromLastClose { get; set; }
        public Double PercentChangeFromLastClose { get; set; }
        public Double Volume { get; set; }
        public Double SplitRatio { get; set; }
        public Double LastAdjusted { get; set; }
        public Double OpenAdjusted { get; set; }
        public Double LastCloseAdjusted { get; set; }
        public Double HighAdjusted { get; set; }
        public Double LowAdjusted { get; set; }
        public Double ChangeFromOpenAdjusted { get; set; }
        public Double ChangeFromLastCloseAdjusted { get; set; }
        public Double VolumeAintdjusted { get; set; }
        public bool NotTraded { get; set; }
        public string Outcome { get; set; }
        public string Message { get; set; }
        public string Identity { get; set; }
        public int Delay { get; set; }
    }

You can learn more here: JSON serialization and deserialization in Silverlight

Use Newtonsoft Json.net library to build a dictionary by your JSON. Then use LINQ.

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