1

I have the following json string

{
  "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
 }
}

I also created the following classes

 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

How can I extract the values for Buying and Selling using newtonsoft.json? I have been looking at and trying examples for the past 4 hours and I am still no closer to an answer. I am very new to json

please help

Craig
  • 79
  • 9
  • [Deserialize it?](http://stackoverflow.com/a/20080586/3740093) – Visual Vincent Apr 03 '16 at 15:04
  • If you want to *parse* it, you dont need those classes, you can pluck out the values individually. If you want to *deserialize* it, use the classes. Not clear what you want. – Ňɏssa Pøngjǣrdenlarp Apr 03 '16 at 15:04
  • @Plutonix : Either way deserialization would probably still be the easiest though. ;) – Visual Vincent Apr 03 '16 at 15:06
  • maybe someone could provide a code sample? – Craig Apr 03 '16 at 15:08
  • I just gave you a link in my first comment. – Visual Vincent Apr 03 '16 at 15:09
  • @VisualVincent Not really, if the OP only needs 2 values ever, parsing out 2 vars is pretty simple and means you can get rid of the classes and not have object references to deal with. – Ňɏssa Pøngjǣrdenlarp Apr 03 '16 at 15:09
  • A sample of *what???* Parsing or Deserializing? Do you only ever want those 2 values? Or is there other info there which is of value? – Ňɏssa Pøngjǣrdenlarp Apr 03 '16 at 15:10
  • @Plutonix : But for the simplicity I'd say it would be simpler to deserialize, _as it stands_. Since he's already made the classes and apparently have prepared the Newtonsoft references. The classes don't have any unmanaged data so they don't need to implement the IDisposable interface. Well, what do I know? I've never used JSON. ;) – Visual Vincent Apr 03 '16 at 15:12
  • I want those two values only. I would appreciate if someone can show me how to do it. Every time I try and deserialise it I get an error stating: System.NullReferenceException{"Object reference not set to an instance of an object."} – Craig Apr 03 '16 at 15:16
  • Errors and code are good to show in the beginning you know. As I've never used JSON I can currently not help you parse nor deserialize it other than providing links. – Visual Vincent Apr 03 '16 at 15:20
  • An error message with no code is not very useful – Ňɏssa Pøngjǣrdenlarp Apr 03 '16 at 15:25

2 Answers2

1

Apparently your code does not desertizare correctly because of the way you define the arrays. Putting it in the form of Public Property data As List(Of Datum) (and so on) instead of Public Property data() As Datum solves the issue.

The full and tested code can be found here. This is how you deserialize the json text:

Sub Main()

    Dim jsonAsText = File.ReadAllText("C:\Path\To\json.txt")
    Dim deserializedRootObject = Newtonsoft.Json.JsonConvert.DeserializeObject(Of RootObject)(jsonAsText)

End Sub

This deserialization uses RootObject and its sub-objects, which are shown here:

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
Xavier Peña
  • 7,399
  • 9
  • 57
  • 99
0

You can try example Bellow taken from newtonsoft.com

    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

Amir
  • 125
  • 1
  • 6
  • Thanks Amir, but I have been looking and trying to get it working all day looking at examples such as the one you posted. Is there anybody here who could maybe provide a code sample to extract the values from the json string I provided? – Craig Apr 03 '16 at 15:28
  • @Craig The json is not just a fancy string you extract info from - it is serialized data. You either need to deserialize it to an an object and get the prop vals, or parse it to pluck out the parts you want. – Ňɏssa Pøngjǣrdenlarp Apr 03 '16 at 15:35
  • @Craig I have provided the full working code in my answer (see answer below). And I've figured out what didn't work in the first place with your code. – Xavier Peña Apr 03 '16 at 15:45