1

I have a JSON object that I store in a database that is created by var data = JSON.stringify($('#frm').serializeArray()); I want to call it and use Newtonsoft.json to parse it out into it's parts. My totally convoluted VB.Net code is (Dont pick on me I know it is a hack)

     Dim reader As JsonTextReader = New JsonTextReader(New StringReader(TextBox1.Text))
    Dim firstname As String = ""
    Dim middleInitial As String = ""
    Dim lastName As String = ""


    While (reader.Read())

        Select Case reader.Value
            Case "disFirst"
                reader.Read()
                reader.Read()
                firstname = reader.Value
            Case "disMiddle"
                reader.Read()
                reader.Read()
                middleInitial = reader.Value
            Case "disLast"
                reader.Read()
                reader.Read()
                lastName = reader.Value
        End Select
    End While

    MessageBox.Show(firstname + " " + middleinitial + " " + lastName)

but it works. I 100% know that there are MUCH better ways so if someone can point me in the right VB.Net direction that would be great. My Json string is

    [{"name":"disFirst","value":"Robert"},{"name":"disMiddle","value":"S"},{"name":"disLast","value":"Smith"},{"name":"disSuffix","value":""},{"name":"disEmail","value":"[email protected], [email protected]"},{"name":"disAffiliations","value":"Arizona\r\nXXXX"},{"name":"disPMIDlist","value":""},{"name":"disThreshholdFactor","value":""}]
7
  • That code is not really parsing it (JObject.Parse(jstr)). That seems like it should deserialize to an array pretty easily. Seems a bad structure though since the names arent related to each other. "Robert" is not linked or related to "Smith" Commented Nov 22, 2016 at 1:44
  • I've gotten closer. the structure I can't control since it is a standard jQuery serialize of a form. What I've gotten to is Commented Nov 22, 2016 at 1:54
  • Sorry I don't know how to add code in the add comment Commented Nov 22, 2016 at 1:55
  • While (reader.Read()) If reader.TokenType = JsonToken.StartObject Then Dim obj As JObject = JObject.Load(reader) Commented Nov 22, 2016 at 1:57
  • then If obj("name") = "disFirst" Then firstname = obj("value") End If Commented Nov 22, 2016 at 1:58

1 Answer 1

1

That json is more that a little wonky. It appears to be describing an entity, but rather than an object with firstname, lastname etc it is just a set of name pairs. As long as there is just one entity there, I guess it could work.

This will deserialize the json to an array of name pairs:

Public Class NameValuePair
    Public Property name As String
    Public Property value As String
End Class

Then to deserialize:

Dim jstr = from whereever
Dim myNVPs = JsonConvert.DeserializeObject(Of NameValuePair())(jstr)

The name will contain those awful keys like disLast and value will be the related data. As an array you can loop thru it to get the data.

Sign up to request clarification or add additional context in comments.

2 Comments

perfect- works like a charm. the ugly names like disFirst and disLast are strange because the dis identifies the form that they are on. There are other places in the application that call for first name and last name etc. the the JSON is created automatically by JSON.stringify($('#frm').serializeArray()); using the name of the input element
It is more common for those to come back already as some sort of unified object like Person {FIrst, Last, Middle, Gender, BirthDate...etc} then you would not have to piece it together and could pass more than one at a time if needed. Glad it worked

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.