本文介绍了不带密钥反序列化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我该如何反序列化?
{"bids":[[15575.35,2.44],[15567.47,2.06],[15567.07,4.68],[15563,0.11240254]],
"asks":[[16493.08,3.22487788],[16498.86,0.01864],[16550,0.0756622],[16650,0.00182419]]}
我的代码:
string remoteUri = "https://bitbay.net/API/Public/BTCPLN/orderbook.json";
WebClient myWebClient = new WebClient();
var json = myWebClient.DownloadString(remoteUri);
JavaScriptSerializer js = new JavaScriptSerializer();
var test = js.Deserialize<OrderBook>(json);
class OrderBook
{
public List<Order> bids { get; set; }
public List<Order> asks { get; set; }
}
public class Order
{
public Double rate { get; set; }
public Double amount { get; set; }
}
我有此错误: System.Web.Extensions.dll中出现未处理的'System.InvalidOperationException'类型的异常
I have this error: An unhandled exception of type 'System.InvalidOperationException' occurred in System.Web.Extensions.dll
其他信息:数组反序列化不支持类型"GetValue.Class.Order".
Additional information: Type 'GetValue.Class.Order' is not supported for deserialization of an array.
推荐答案
看看这个网站 quicktype.io ,它将为您提供以下完整示例
Take a look at this site quicktype.io, it will provide you with the following full sample
namespace QuickType
{
using System;
using System.Net;
using System.Collections.Generic;
using Newtonsoft.Json;
public partial class Quote
{
[JsonProperty("asks")]
public List<List<double>> Asks { get; set; }
[JsonProperty("bids")]
public List<List<double>> Bids { get; set; }
}
public partial class Quote
{
public static Quote FromJson(string json) =>
JsonConvert.DeserializeObject<Quote>(json, Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Quote self) =>
JsonConvert.SerializeObject(self, Converter.Settings);
}
public class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
};
}
}
要反序列化您的Json
,只需致电
to deserialize your Json
just call
var remoteUri = "https://bitbay.net/API/Public/BTCPLN/orderbook.json";
var myWebClient = new WebClient();
var json = myWebClient.DownloadString(remoteUri);
var quote = Quote.FromJson(json);
这篇关于不带密钥反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!