用newtonsot.Json在vb.net中解析Json



我有以下json字符串

{
  "extractorData" : {
"url" : "http://absa.co.za/Absacoza/Indices/Absa-Exchange-Rates",
"resourceId" : "4dd52d44301ebd50db87975bf5df9711",
"data" : [ {
  "group" : [ {
    "absa" : [ {
      "text" : "USD"
    } ]
  }, {
    "absa" : [ {
      "text" : "US Dollar"
    } ]
  }, {
    "absa" : [ {
      "text" : "*"
    } ]
  }, {
    "absa" : [ {
      "text" : "14.429"
    } ],
    "Buying" : [ {
      "text" : "14.429"
    } ]
  }, {
    "absa" : [ {
      "text" : "14.409"
    } ]
  }, {
    "absa" : [ {
      "text" : "14.404"
    } ]
  }, {
    "absa" : [ {
      "text" : "14.8323"
    } ],
    "Selling" : [ {
      "text" : "14.8323"
    } ]
  }, {
    "absa" : [ {
      "text" : "14.8373"
    } ]
  } ]
} ]
},
"pageData" : {
"statusCode" : 200,
"timestamp" : 1459675946038
 }
}

我还创建了以下类

 Public Class Rootobject
    Public Property extractorData As Extractordata
    Public Property pageData As Pagedata
 End Class
Public Class Extractordata
    Public Property url As String
    Public Property resourceId As String
    Public Property data() As Datum
End Class
Public Class Datum
    Public Property group() As Group
End Class
Public Class Group
    Public Property absa() As Absa
    Public Property Buying() As Buying
    Public Property Selling() As Selling
End Class
Public Class Absa
    Public Property text As String
End Class
Public Class Buying
    Public Property text As String
End Class
Public Class Selling
    Public Property text As String
End Class
Public Class Pagedata
    Public Property statusCode As Integer
    Public Property timestamp As Long
End Class

如何使用newtonsoft.json提取Buying和Selling的值?在过去的4个小时里,我一直在研究和尝试例子,但我仍然离答案不远。我对json 很陌生

请帮助

显然,由于定义数组的方式,您的代码没有正确放弃。以Public Property data As List(Of Datum)(依此类推)的形式而不是Public Property data() As Datum解决了这个问题。

完整的和经过测试的代码可以在这里找到。这就是反序列化json文本的方式:

Sub Main()
    Dim jsonAsText = File.ReadAllText("C:PathTojson.txt")
    Dim deserializedRootObject = Newtonsoft.Json.JsonConvert.DeserializeObject(Of RootObject)(jsonAsText)
End Sub

此反序列化使用RootObject及其子对象,如下所示:

Public Class Absa
    Public Property text As String
End Class
Public Class Buying
    Public Property text As String
End Class
Public Class Selling
    Public Property text As String
End Class
Public Class Group
    Public Property absa As List(Of Absa)
    Public Property Buying As List(Of Buying)
    Public Property Selling As List(Of Selling)
End Class
Public Class Datum
    Public Property group As List(Of Group)
End Class
Public Class ExtractorData
    Public Property url As String
    Public Property resourceId As String
    Public Property data As List(Of Datum)
End Class
Public Class PageData
    Public Property statusCode As Integer
    Public Property timestamp As Long
End Class
Public Class RootObject
    Public Property extractorData As ExtractorData
    Public Property pageData As PageData
End Class

您可以尝试newtonsoft.com 中的Bellow示例

    string json = @"{
    'Email': 'james@example.com',
    'Active': true,
    'CreatedDate': '2013-01-20T00:00:00Z',
    'Roles': [
      'User',
     'Admin'
    ]
    }";
    Account account = JsonConvert.DeserializeObject<Account>(json);
    Console.WriteLine(account.Email);

http://www.newtonsoft.com/json/help/html/deserializeobject.htm

相关内容

  • 没有找到相关文章

最新更新