问题描述
目前,我正在尝试反序列化在Silverlight以下JSON输出无解串器的JavaScript。我听说有办法做到这一点使用JsonArray和LINQ,但我不能完全弄清楚。
{
安全:{
CIK:0000789019,
CUSIP:594918104,
符号:MSFT,
ISIN:US5949181045,
Valoren:951692,
名称:微软公司,
市场:纳斯达克交易代码,
CategoryOrIndustry:技术,
结果:成功,
消息 :空,
标识:空,
延迟:0
},
开始日期:2011/7/1,
结束日期:2011年7月6日,
行情:[{
日期:7/5/2011,
上:26.03,
开放:26.1,
LastClose:26.02,
高:26.15,
低:25.9,
ChangeFromOpen:-0.07,
PercentChangeFromOpen:-0.268,
ChangeFromLastClose:0.01,
PercentChangeFromLastClose:0.038,
音量: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:虚假,
结果:成功 ,
消息:空,
标识:空,
延迟:0
},{
日期:7/1 / 2011,
上:26.02,
打开:25.93,
LastClose:26,
高:26.17,
低 :25.84,
ChangeFromOpen:0.09,
PercentChangeFromOpen:0.347,
ChangeFromLastClose:0.02,
PercentChangeFromLastClose:0.077,
音量 :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 :假的,
结果:成功,
消息:空,
标识:空,
延迟:0
}] ,
结果:成功,
消息:空,
身份:曲奇,
延迟:0.014001
}
会怎么做这样的事情。我希望提取<$ C的最佳方法$ C>日期和 LastAdjusted
从行情
阵列,并把每一个到自己的数组?
下面是我得到多远得到JSON到流:
downloader.OpenReadCompleted + =新OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
downloader.OpenReadAsync(serviceUri);
}
无效downloader_OpenReadCompleted(对象发件人,OpenReadCompletedEventArgs E)
{
如果(e.Error == NULL)
{
流responseStream = e.Result;这里
//添加代码
}
}
有关Silverlight的下面的代码将一个JSON字符串,通过引号来提取日期和LastAdjusted上市的列表它反序列化到列表对象,然后循环。确保到System.ServiceModel.Web参考,以访问的(MSDN)班。
C#
字符串JSON = //你的JSON字符串
清单myListing = DeserializeJSON(JSON);
的foreach(在listing.Quotes报价报价)
{
DateTime的DT = quote.Date;
双击lastAdjusted = quote.LastAdjusted;
}
上市DeserializeJSON(JSON字符串)
{使用
(MemoryStream的毫秒=新的MemoryStream(Encoding.Unicode.GetBytes(JSON)))
{
DataContractJsonSerializer序列化=新DataContractJsonSerializer(typeof运算(上市));
回报率(上市)serializer.ReadObject(MS);
}
}
类
公共类上市
{
公安部安全防范{搞定;组; }
公共DateTime的起始日期{搞定;组; }
公共DateTime的结束日期{搞定;组; }
公开名单<&报价GT;行情{搞定;组; }
}
公共类安全
{
公共字符串CIK {搞定;设置;}
公共字符串CUSIP {搞定;设置;}
公共字符串符号{搞定;设置;}
公共字符串ISIN {搞定;设置;}
公共字符串Valoren {搞定;设置;}
公共字符串名称{;设置;}
公共字符串市场{搞定;设置;}
公共字符串CategoryOrIndustry {搞定;设置;}
公共字符串结果{搞定;设置;}
公共字符串消息{搞定;设置;}
公共字符串身份{获得;设置;}
公共字符串延迟{搞定;组; }
}
公共类报价
{
公众的DateTime日期{搞定;组; }
公共双尾{搞定;组; }
公共双开{搞定;组; }
公共双LastClose {搞定;组; }
公共双高{搞定;组; }
公共双低{搞定;组; }
公共双ChangeFromOpen {搞定;组; }
公共双PercentChangeFromOpen {搞定;组; }
公共双ChangeFromLastClose {搞定;组; }
公共双PercentChangeFromLastClose {搞定;组; }
公共双量均{搞定;组; }
公共双SplitRatio {搞定;组; }
公共双LastAdjusted {搞定;组; }
公共双OpenAdjusted {搞定;组; }
公共双LastCloseAdjusted {搞定;组; }
公共双HighAdjusted {搞定;组; }
公共双LowAdjusted {搞定;组; }
公共双ChangeFromOpenAdjusted {搞定;组; }
公共双ChangeFromLastCloseAdjusted {搞定;组; }
公共双VolumeAintdjusted {搞定;组; }
公共BOOL NotTraded {搞定;组; }
公共字符串结果{搞定;组; }
公共字符串消息{搞定;组; }
公共字符串身份{获得;组; }
公众诠释延迟{搞定;组; }
}
您可以在这里了解更多:的
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
这篇关于使用asp.net库反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!