序列化名为"return"的JSON对象. [英] Serialize JSON object named "return"

查看:83
本文介绍了序列化名为"return"的JSON对象.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试针对Mt Gox的Http API编写代码.它返回如下所示的JSON:

I'm trying to write a ticker against Mt Gox's Http API. It returns JSON that looks like this:

{
"result":"success",
"return":
 {
 "high": {"value":"5.70653","value_int":"570653","display":"$5.70653","currency":"USD"},
 "low": {"value":"5.4145","value_int":"541450","display":"$5.41450","currency":"USD"},
 "avg": {"value":"5.561119626","value_int":"556112","display":"$5.56112","currency":"USD"},
 "vwap": {"value":"5.610480461","value_int":"561048","display":"$5.61048","currency":"USD"},
 "vol": {"value":"55829.58960346","value_int":"5582958960346","display":"55,829.58960346\u00a0BTC","currency":"BTC"},
 "last_all":{"value":"5.5594","value_int":"555940","display":"$5.55940","currency":"USD"},
 "last_local":{"value":"5.5594","value_int":"555940","display":"$5.55940","currency":"USD"},
 "last_orig":{"value":"5.5594","value_int":"555940","display":"$5.55940","currency":"USD"},
 "last":{"value":"5.5594","value_int":"555940","display":"$5.55940","currency":"USD"},
 "buy":{"value":"5.53587","value_int":"553587","display":"$5.53587","currency":"USD"},
 "sell":{"value":"5.56031","value_int":"556031","display":"$5.56031","currency":"USD"}
 }
}

我正在尝试将该信息转换为对象.我制作了一组看起来像这样的类:

I'm trying to cast that information into an object. I've made a set of classes that look like this:

[DataContract]
class MtGoxResponse
{
    public string result { get; set; }
    [DataMember(Name="return")]
    public Resp Resp { get; set; }
}

[DataContract]
class Resp
{
    public HLA high { get; set; }
    public HLA low { get; set; }
    public HLA avg { get; set; }
    public HLA vwap { get; set; }
    public HLA vol { get; set; }
    public HLA last_all { get; set; }
    public HLA last_local { get; set; }
    public HLA last_orig { get; set; }
    public HLA last { get; set; }
    public HLA buy { get; set; }
    public HLA sell { get; set; }
}

[DataContract]
class HLA
{
    public double value { get; set; }
    public int value_int { get; set; }
    public string display { get; set; }
    public string currency { get; set; }
}

Result每次都可以通过,但Resp始终为空.我是否缺少使用DataContract属性的内容?我假设根本原因是对象的名称,但是肯定有解决方法.

Result comes through fine every time, but Resp is always null. Am I missing something with the DataContract attributes? I'm assuming the root cause is the object's name, but surely there's a way around it.

推荐答案

我不确定您的[DataMember]属性为什么不起作用.根据我的阅读,[DataMember]对于每种序列化程序的实现似乎都有不同的解释,因此它很可能是一个错误.

I'm not sure exactly why your [DataMember] attribute isn't working. From what I've read, [DataMember] appears to be interpreted differently per implementation of serializers, so it may well be a bug.

但是,您可以通过简单地在return之前使用@符号来消除使用它的需要,就像这样:

However, you can remove the need of using it by simply using the @ sign before return, like so:

[DataContract]
class MtGoxResponse
{
    public string result { get; set; }
    public Resp @return { get; set; }
}

在MSDN页面上,对于 C#关键字,该前缀有些被动地提及.

This prefix is mentioned somewhat passively on MSDN's page for C# keywords.

这篇关于序列化名为"return"的JSON对象.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆