0

I'm using Newtonsoft.JSon.Linq and at this point im kinda confused about how to make things work in the way i need it for my project. In order to jump into topic heres what im doing or at least want to do.

I want to extract the JSon data into structures based on the second level of the JSon nodes for a JSon which has this structure:

{
"country" : {
    "germany" : {
        "capital" : "berlin",
        "inhabitants_in_million" : 82,
        "zip-codes" : [
            [
                "berlin",
                10115

            ],
            [
                "hamburg",
                21145
            ],
            [
                "munich",
                80331
            ]
        ]
        },
    "france" : {
        "capital" : "paris",
        "inhabitants_in_million" : 67,
        "zip-codes" : [
            [
                "paris",
                75001
            ],
            [
                "marseille",
                13000
            ]
        ]
    }
}

My Structure so far is like that so far:

    Public countries As List(Of country)

Public Structure country

    Public name As String
    Public capital As String
    Public inhabtitants As Integer

End Structure

And im using this code to read the data of the JSon:

Private Sub json_sub()
    For Each n In json_object
        For Each n_2 As JProperty In n.Value
            Dim c As New country

            c.name = n_2.Name
            c.capital = json_object.SelectToken("country").SelectToken(n_2.Name).SelectToken("capital")
            c.inhabtitants = json_object.SelectToken("country").SelectToken(n_2.Name).SelectToken("inhabitants_in_million")

            countries.Add(c)

        Next
    Next

End Sub

But im somehow struggling with the data serialized in [] since its an array and all my attempts so far did not work off. I browsed for quite some time and read about deserializiation but unfortunately im kinda stuck at this point now.

My goal is to add the values from this arrays into a string which should look like:

berlin,10115;hamburg,21145;munich,80331

So I can add this string to my structure country with

Public zip_codes As String

Instead of using an array which is unhandy in my project at this point. I hope anyone can get me suggestion how i can achieve this.

Sincerly, boon

6
  • Copy that json into your clipboard, and then in visual studio go to Edit > Paste Special > Paste JSON As Classes. That should make a class you can deserialize the json string straight into, and access its properties by name. Then you can glue whatever properties you want together in a string, or better yet add a property to the class that returns the string you want Commented Jun 5, 2017 at 12:44
  • the actual json is like 25mb in size the one i posted here is just an example of how the json looks like in dimensions the program acutally reads the json from the *.json and converts it to an object so how do i proceed in this case since importing the json as whole does not work for me as i need to import the json on a dayly base to update a sql database Commented Jun 5, 2017 at 12:49
  • it also reults about a few million conflicts due to creating namespaces multiple times since there are a few more nodes above the and the json contains multiple datasets for every "country" in each of those Commented Jun 5, 2017 at 12:57
  • What you are trying to do in that code is create a collection by parsing it - easier to deserialize it. That JSON is invalid by the way. Deserialize using JSon Commented Jun 5, 2017 at 14:09
  • i edited the json Commented Jun 5, 2017 at 15:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.