2013-12-22 25 views
1

我有一个JSON响应,我需要解析到ASP.Net(Vb.net或c#)中的对象,但我没有看到任何示例嵌套的响应字符串以及如何解析(仅限简单值对)。如何解析嵌套的JSON响应asp.net(vb.net)

这里有一个:

{ 
    "ticker": { 
     "high": 3.494, 
     "low": 2.9, 
     "avg": 3.197, 
     "vol": 463260.58724, 
     "vol_cur": 143878.12481, 
     "last": 2.924, 
     "buy": 2.959, 
     "sell": 2.925, 
     "updated": 1387635241, 
     "server_time": 1387635242 
    } 
} 

从一个站点,而另一个位置:

{ 
    "result": "success", 
    "return": { 
     "high": { 
      "value": "745.00000", 
      "value_int": "74500000", 
      "display": "$745.00", 
      "display_short": "$745.00", 
      "currency": "USD" 
     }, 
     "low": { 
      "value": "610.00000", 
      "value_int": "61000000", 
      "display": "$610.00", 
      "display_short": "$610.00", 
      "currency": "USD" 
     }, 
     "avg": { 
      "value": "664.21299", 
      "value_int": "66421299", 
      "display": "$664.21", 
      "display_short": "$664.21", 
      "currency": "USD" 
     }, 
     "vwap": { 
      "value": "658.47213", 
      "value_int": "65847213", 
      "display": "$658.47", 
      "display_short": "$658.47", 
      "currency": "USD" 
     }, 
     "vol": { 
      "value": "29333.04107565", 
      "value_int": "2933304107565", 
      "display": "29,333.04 BTC", 
      "display_short": "29,333.04 BTC", 
      "currency": "BTC" 
     }, 
     "last_local": { 
      "value": "645.00000", 
      "value_int": "64500000", 
      "display": "$645.00", 
      "display_short": "$645.00", 
      "currency": "USD" 
     }, 
     "last_orig": { 
      "value": "645.00000", 
      "value_int": "64500000", 
      "display": "$645.00", 
      "display_short": "$645.00", 
      "currency": "USD" 
     }, 
     "last_all": { 
      "value": "645.00000", 
      "value_int": "64500000", 
      "display": "$645.00", 
      "display_short": "$645.00", 
      "currency": "USD" 
     }, 
     "last": { 
      "value": "645.00000", 
      "value_int": "64500000", 
      "display": "$645.00", 
      "display_short": "$645.00", 
      "currency": "USD" 
     }, 
     "buy": { 
      "value": "638.36000", 
      "value_int": "63836000", 
      "display": "$638.36", 
      "display_short": "$638.36", 
      "currency": "USD" 
     }, 
     "sell": { 
      "value": "644.98500", 
      "value_int": "64498500", 
      "display": "$644.99", 
      "display_short": "$644.99", 
      "currency": "USD" 
     }, 
     "item": "BTC", 
     "now": "1387644090735676" 
    } 
} 

我下载Json.Net(看起来不错),但它看起来像它仅支持非嵌套JSON字符串(至少是例子)。他们显示数组,但这些不是数组。

我想过使用字符串操作和正则表达式进行一种手动解析,但宁愿有我可以重复使用的东西。只是不知道从哪里开始。

+0

啊!这是节省时间的一件事:http:// json2csharp。COM/ – MC9000

+0

试过,但得到的数据出来工作不 昏暗JSS =新JavaScriptSerializer() 昏暗OT作为对象= oReturn(0) 昏暗oReturn = jss.Deserialize(的对象)(httpdata) – MC9000

+0

人?这是反序列化JSON字符串的错误方法吗? – MC9000

回答

2

为了您的第一个例子,如果你有一个像这样的类(由json2csharp.com生成):

public class RootObject 
{ 
    public Ticker ticker { get; set; } 
} 

public class Ticker 
{ 
    public double high { get; set; } 
    public double low { get; set; } 
    public double avg { get; set; } 
    public double vol { get; set; } 
    public double vol_cur { get; set; } 
    public double last { get; set; } 
    public double buy { get; set; } 
    public double sell { get; set; } 
    public int updated { get; set; } 
    public int server_time { get; set; } 
} 

那么你可以反序列化为他们这样使用Json.Net:

RootObject obj = JsonConvert.DeserializeObject<RootObject>(json); 

对于第二个例子,你可以定义你的类是这样的:

public class RootObject2 
{ 
    public string result { get; set; } 
    public Return @return { get; set; } 
} 

public class Return 
{ 
    public Item high { get; set; } 
    public Item low { get; set; } 
    public Item avg { get; set; } 
    public Item vwap { get; set; } 
    public Item vol { get; set; } 
    public Item last_local { get; set; } 
    public Item last_orig { get; set; } 
    public Item last_all { get; set; } 
    public Item last { get; set; } 
    public Item buy { get; set; } 
    public Item sell { get; set; } 
    public string item { get; set; } 
    public string now { get; set; } 
} 

public class Item 
{ 
    public string value { get; set; } 
    public string value_int { get; set; } 
    public string display { get; set; } 
    public string display_short { get; set; } 
    public string currency { get; set; } 
} 

和反序列化以相同的方式:

RootObject2 obj = JsonConvert.DeserializeObject<RootObject2>(json2); 
+0

谢谢Brian!对不起,我仍然在发布答案的同时格式化我的发现。我是JSON noob,但学习! – MC9000

1

好的,明白了。有很多不同的方法可以做到这一点,但首先我必须正确创建类才能使其发挥作用。我去了json2csharp.com并粘贴返回JSON的URL(或者,粘贴一个JSON字符串) - 这会自动创建您的类(当然,您也可以手动输入它们),这很好。 在我的第一个例子中,这些类如下所示(在VB.Net):

Namespace BTCE 
#Region "BTCE response classes" 
    Public Class Ticker 
     Public Property high() As Double 
      Get 
       Return m_high 
      End Get 
      Set(value As Double) 
       m_high = value 
      End Set 
     End Property 
     Private m_high As Double 
     Public Property low() As Double 
      Get 
       Return m_low 
      End Get 
      Set(value As Double) 
       m_low = value 
      End Set 
     End Property 
     Private m_low As Double 
     Public Property avg() As Double 
      Get 
       Return m_avg 
      End Get 
      Set(value As Double) 
       m_avg = value 
      End Set 
     End Property 
     Private m_avg As Double 
     Public Property vol() As Double 
      Get 
       Return m_vol 
      End Get 
      Set(value As Double) 
       m_vol = value 
      End Set 
     End Property 
     Private m_vol As Double 
     Public Property vol_cur() As Double 
      Get 
       Return m_vol_cur 
      End Get 
      Set(value As Double) 
       m_vol_cur = value 
      End Set 
     End Property 
     Private m_vol_cur As Double 
     Public Property last() As Double 
      Get 
       Return m_last 
      End Get 
      Set(value As Double) 
       m_last = value 
      End Set 
     End Property 
     Private m_last As Double 
     Public Property buy() As Double 
      Get 
       Return m_buy 
      End Get 
      Set(value As Double) 
       m_buy = value 
      End Set 
     End Property 
     Private m_buy As Double 
     Public Property sell() As Double 
      Get 
       Return m_sell 
      End Get 
      Set(value As Double) 
       m_sell = value 
      End Set 
     End Property 
     Private m_sell As Double 
     Public Property updated() As Integer 
      Get 
       Return m_updated 
      End Get 
      Set(value As Integer) 
       m_updated = value 
      End Set 
     End Property 
     Private m_updated As Integer 
     Public Property server_time() As Integer 
      Get 
       Return m_server_time 
      End Get 
      Set(value As Integer) 
       m_server_time = value 
      End Set 
     End Property 
     Private m_server_time As Integer 
    End Class 

    Public Class RootObject 
     Public Property ticker() As Ticker 
      Get 
       Return m_ticker 
      End Get 
      Set(value As Ticker) 
       m_ticker = value 
      End Set 
     End Property 
     Private m_ticker As Ticker 
    End Class 
#End Region 

End Namespace 

重要! - 注意 “RootObject” 类获得/套的股票对象 使用JavaScriptSerializer,代码看起来像这样:

Dim jss = New JavaScriptSerializer() 
    Dim oReturn As BTCE.RootObject = jss.Deserialize(Of BTCE.RootObject)(httpdata) 
    Dim avg As String = oReturn.ticker.avg 
    Dim high as String = oReturn.ticker.high 
    ... and so forth 

使用Newtonsoft.Json库(json.net):

Dim ro As BTCE.RootObject = JsonConvert.DeserializeObject(Of BTCE.RootObject)(httpdata) 
Dim avg As String = ro.ticker.avg 
Dim high As String = ro.ticker.high 
... and so forth 

这是输入在这种情况下,httpdata字符串是:

{"ticker":{"high":3.494,"low":2.9,"avg":3.197,"vol":463260.58724,"vol_cur":143878.12481,"last":2.924,"buy":2.959,"sell":2.925,"updated":1387635241,"server_time":1387635242}} 

这是我首次涉足JSON转换成可用的东西,但在散热片有非常一点运气后在互联网上找到复杂的例子(嵌套的JSON),我现在明白了,希望这对DotNetter无处不在。在我的示例中,我有一个应用程序,用于轮询网站的JSON数据,将其转换为可用对象,进行一些处理,并最终输出(更多)JSON数据(以及XML和其他格式)作为WCF Web服务。

+0

看起来你已经掌握了它。关于json2csharp.com的一个注意事项 - 它可以作为一个很好的起点,但正如您可能注意到的那样,它不能识别出您可以重复使用同一个类来实现多种用法的情况。你的第二个例子就是一个例子:json2csharp为每个不同的属性'high','low','avg'生成一个单独的类,尽管它们都具有相同的结构。所以只需记住一些事情。通常你可以简化它给你的东西。 –