Avatar billede steensommer Praktikant
28. august 2015 - 10:58 Der er 14 kommentarer

JSON

Hej

Jeg har en for mig ret svær opgave der lyder sådan her:

Personer skal fra en webside indtaste diverse oplysninger om dem selv. Når det er gjort skal data hentes vha JSON.
JSON har jeg overhovedet ingen erfaringer med og er lidt på bar bund.
Tanken er at et windowsform skal kunne hente dagens indtastninger/JSON, hvilket nogenlunde foregår herfra:

http://voresside.kundeside.dk/json?key=t6%$VAKs

Vi modtager følgende JSON.JSON med følgende indhold:

"name": "B Nrg",
            "reg_nr": "",
            "cpr_nr": "1",
            "address": "1",
            "phone": "1",
            "mobile": "1",
            "workphone": "1",
            "email": "borg@homail.com",   
            "municipality": "1",
            "region": "1",
            "country": "1",
            "city": "1",
            "caregiver_address": "1",
            "caregiver_phone": "1",
            "caregiver_mobile": "1",
            "patient_homecare": "0",
            "patient_helpfor": "",
            "patient_homecare_phone": "",
            "patient_treatment": "0",
            "patient_pigs": "0",
            "patient_mrsa_positive": "0",
            "patient_allergic": "0",
            "patient_allergic_to": "",
            "patient_allergic_symptoms": "",
            "patient_height": "1",
            "patient_weight": "1",
            "patient_smoke": "0",
            "patient_smoke_amount": "",
            "patient_alcohol": "0",
            "patient_alcohol_amount": "",
            "patient_medication": "0",
            "patient_medication_amount": "",
            "patient_diseases": "0",
            "patient_diseases_type": "",
            "patient_pacemaker": "0",
            "gp_address": "1",
            "gp_city": "1",
            "gp_phone": "1",
            "hospital_address": "1",       
            "hospital_city": "1",
            "may_obtain_information": "0",
            "journal": "1",
            "completed": "1",
            "patient_image": "http://voresside.kundeside.dk/media/1012/e-dynamicwebnet-patientformphvardecom-media-_basque-country.png"
        },
        {
            "name": "S Som",
            "reg_nr": "",
            "cpr_nr": "",
            "address": "",
            "phone": "",
            "mobile": "",
            "workphone": "",
            "email": "ster@gail.com",   
            "municipality": "",
            "region": "",
            "country": "",
            "city": "",
            "caregiver_address": "",
            "caregiver_phone": "",
            "caregiver_mobile": "",
            "patient_homecare": "0",
            "patient_helpfor": "",
            "patient_homecare_phone": "",
            "patient_treatment": "0",
            "patient_pigs": "0",
            "patient_mrsa_positive": "0",
            "patient_allergic": "0",
            "patient_allergic_to": "",
            "patient_allergic_symptoms": "",
            "patient_height": "",
            "patient_weight": "",
            "patient_smoke": "0",
            "patient_smoke_amount": "",
            "patient_alcohol": "0",
            "patient_alcohol_amount": "",
            "patient_medication": "0",
            "patient_medication_amount": "",
            "patient_diseases": "0",
            "patient_diseases_type": "",
            "patient_pacemaker": "0",
            "gp_address": "",
            "gp_city": "",
            "gp_phone": "",
            "hospital_address": "",       
            "hospital_city": "",
            "may_obtain_information": "0",
            "journal": "0",
            "completed": "0",
            "patient_image": "http://voresside.kundeside.dk/media/1012/e-dynamicwebnet-patientformphvardecom-media-_basque-country.png"
        }

Nu er opgaven så at indhente ovenstående data til en Windowsform så jeg kan indsætte data i vores SQL database. Det sidste har jeg ingen problemer med men hvordan SØREN henter jeg JSON til windowsformen??

Vh Steen
Avatar billede arne_v Ekspert
28. august 2015 - 16:19 #1
Parser JSON ind i en data struktur og putter data fra den data struktur ind i din form.

Hvilken JSON parser bruger du?
Avatar billede steensommer Praktikant
28. august 2015 - 18:39 #2
Allerede der er jeg ude på et skråplan.
Jeg har ingen erfaring OVERHOVEDET med JSON
Avatar billede arne_v Ekspert
31. august 2015 - 02:50 #3
Eksempel:

Imports System
Imports System.Collections.Generic
Imports System.Web.Script.Serialization

Namespace E
    Public NotInheritable Class Json
        Public Shared Function Deserialize(json As String) As Object
            Dim ser As New JavaScriptSerializer()
            Return ser.DeserializeObject(json)
        End Function
    End Class
    Public Class Program
        Public Shared Sub Main(args As String())
            Dim jsonstr As String = "[{ ""name"": ""B Nrg""," & vbCr & vbLf & "            ""reg_nr"": """"," & vbCr & vbLf & "            ""cpr_nr"": ""1""," & vbCr & vbLf & "            ""address"": ""1""," & vbCr & vbLf & "            ""phone"": ""1""," & vbCr & vbLf & "            ""mobile"": ""1""," & vbCr & vbLf & "            ""workphone"": ""1""," & vbCr & vbLf & "            ""email"": ""borg@homail.com"",  " & vbCr & vbLf & "            ""municipality"": ""1""," & vbCr & vbLf & "            ""region"": ""1""," & vbCr & vbLf & "            ""country"": ""1""," & vbCr & vbLf & "            ""city"": ""1""," & vbCr & vbLf & "            ""caregiver_address"": ""1""," & vbCr & vbLf & "            ""caregiver_phone"": ""1""," & vbCr & vbLf & "            ""caregiver_mobile"": ""1""," & vbCr & vbLf & "            ""patient_homecare"": ""0""," & vbCr & vbLf & "            ""patient_helpfor"": """"," & vbCr & vbLf & "            ""patient_homecare_phone"": """"," & vbCr & vbLf & "            ""patient_treatment"": ""0""," & vbCr & vbLf & "            ""patient_pigs"": ""0""," & vbCr & vbLf & "            ""patient_mrsa_positive"": ""0""," & vbCr & vbLf & "            ""patient_allergic"": ""0""," & vbCr & vbLf & "            ""patient_allergic_to"": """"," & vbCr & vbLf & "            ""patient_allergic_symptoms"": """"," & vbCr & vbLf & "            ""patient_height"": ""1""," & vbCr & vbLf & "            ""patient_weight"": ""1""," & vbCr & vbLf & "            ""patient_smoke"": ""0""," & vbCr & vbLf & "            ""patient_smoke_amount"": """"," & vbCr & vbLf & "            ""patient_alcohol"": ""0""," & vbCr & vbLf & "            ""patient_alcohol_amount"": """"," & vbCr & vbLf & "            ""patient_medication"": ""0""," & vbCr & vbLf & "            ""patient_medication_amount"": """"," & vbCr & vbLf & "            ""patient_diseases"": ""0""," & vbCr & vbLf & "            ""patient_diseases_type"": """"," & vbCr & vbLf & "            ""patient_pacemaker"": ""0""," & vbCr & vbLf & "            ""gp_address"": ""1""," & vbCr & vbLf & "            ""gp_city"": ""1""," & vbCr & vbLf & "            ""gp_phone"": ""1""," & vbCr & vbLf & "            ""hospital_address"": ""1"",      " & vbCr & vbLf & "            ""hospital_city"": ""1""," & vbCr & vbLf & "            ""may_obtain_information"": ""0""," & vbCr & vbLf & "            ""journal"": ""1""," & vbCr & vbLf & "            ""completed"": ""1""," & vbCr & vbLf & "            ""patient_image"": ""http://voresside.kundeside.dk/media/1012/e-dynamicwebnet-patientformphvardecom-media-_basque-country.png""" & vbCr & vbLf & "        }," & vbCr & vbLf & "        {" & vbCr & vbLf & "            ""name"": ""S Som""," & vbCr & vbLf & "            ""reg_nr"": """"," & vbCr & vbLf & "            ""cpr_nr"": """"," & vbCr & vbLf & "            ""address"": """"," & vbCr & vbLf & "            ""phone"": """"," & vbCr & vbLf & "            ""mobile"": """"," & vbCr & vbLf & "            ""workphone"": """"," & vbCr & vbLf & "            ""email"": ""ster@gail.com"",  " & vbCr & vbLf & "            ""municipality"": """"," & vbCr & vbLf & "            ""region"": """"," & vbCr & vbLf & "            ""country"": """"," & vbCr & vbLf & "            ""city"": """"," & vbCr & vbLf & "            ""caregiver_address"": """"," & vbCr & vbLf & "            ""caregiver_phone"": """"," & vbCr & vbLf & "            ""caregiver_mobile"": """"," & vbCr & vbLf & "            ""patient_homecare"": ""0""," & vbCr & vbLf & "            ""patient_helpfor"": """"," & vbCr & vbLf & "            ""patient_homecare_phone"": """"," & vbCr & vbLf & "            ""patient_treatment"": ""0""," & vbCr & vbLf & "            ""patient_pigs"": ""0""," & vbCr & vbLf & "            ""patient_mrsa_positive"": ""0""," & vbCr & vbLf & "            ""patient_allergic"": ""0""," & vbCr & vbLf & "            ""patient_allergic_to"": """"," & vbCr & vbLf & "            ""patient_allergic_symptoms"": """"," & vbCr & vbLf & "            ""patient_height"": """"," & vbCr & vbLf & "            ""patient_weight"": """"," & vbCr & vbLf & "            ""patient_smoke"": ""0""," & vbCr & vbLf & "            ""patient_smoke_amount"": """"," & vbCr & vbLf & "            ""patient_alcohol"": ""0""," & vbCr & vbLf & "            ""patient_alcohol_amount"": """"," & vbCr & vbLf & "            ""patient_medication"": ""0""," & vbCr & vbLf & "            ""patient_medication_amount"": """"," & vbCr & vbLf & "            ""patient_diseases"": ""0""," & vbCr & vbLf & "            ""patient_diseases_type"": """"," & vbCr & vbLf & "            ""patient_pacemaker"": ""0""," & vbCr & vbLf & "            ""gp_address"": """"," & vbCr & vbLf & "            ""gp_city"": """"," & vbCr & vbLf & "            ""gp_phone"": """"," & vbCr & vbLf & "            ""hospital_address"": """",      " & vbCr & vbLf & "            ""hospital_city"": """"," & vbCr & vbLf & "            ""may_obtain_information"": ""0""," & vbCr & vbLf & "            ""journal"": ""0""," & vbCr & vbLf & "            ""completed"": ""0""," & vbCr & vbLf & "            ""patient_image"": ""http://voresside.kundeside.dk/media/1012/e-dynamicwebnet-patientformphvardecom-media-_basque-country.png""" & vbCr & vbLf & "        }]"
            Console.WriteLine(jsonstr)
            Dim oa As Object() = DirectCast(Json.Deserialize(jsonstr), Object())
            For Each o As Object In oa
                Dim o2 As IDictionary(Of String, Object) = DirectCast(o, IDictionary(Of String, Object))
                Console.WriteLine("{0} {1}", o2("name"), o2("cpr_nr"))
            Next
            Console.ReadKey()
        End Sub
    End Class
End Namespace
Avatar billede steensommer Praktikant
31. august 2015 - 08:57 #4
Imponerende :-)

Jeg får en fejl ved:           
Dim ser As New JavaScriptSerializer()

"Type JavaScriptSerializer is not defined"
Avatar billede steensommer Praktikant
31. august 2015 - 11:57 #5
OK ...der mangle en reference til System.Web.Extensions :-)
Avatar billede steensommer Praktikant
31. august 2015 - 12:28 #6
Det er lidt langhåret for mig :-/

Hvis jeg ønsker at hver information anbringes i et tekstfelt i en windows-formular
- Hvad gør jeg
- Hvor skal din kode placeres?
Avatar billede arne_v Ekspert
31. august 2015 - 18:40 #7
Paa samme maade som du putter anden information i tekstfelter - du tildeler dem til Text property.

tfname.Text = o2("name")
tfcprnr.Text = o2("cpr_nr")
etc.
Avatar billede steensommer Praktikant
01. september 2015 - 07:21 #8
Jeg tænkte nu på Namespace E og  Public NotInheritable Class Json?
Avatar billede steensommer Praktikant
01. september 2015 - 10:38 #9
Nå jeg fik da noget til at fungere :-)

Følgende fungerer delvist MEN jeg får kun oplysninger fra det sidste loop. Hvordan kan jeg styre det så jeg primært får nr 1 og dernæst med en "Button" kan bede om den næste indtil der ikke er flere??



Imports System
Imports System.Collections.Generic
Imports System.Web.Script.Serialization

Public Class FrmPatientFormular

    Private Sub HentJSon()
        Dim jsonstr As String = "[{ ""name"": ""B Nrg""," & vbCr & vbLf & "            ""reg_nr"": """"," & vbCr & vbLf & "            ""cpr_nr"": ""1""," & vbCr & vbLf & "            ""address"": ""1""," & vbCr & vbLf & "            ""phone"": ""1""," & vbCr & vbLf & "            ""mobile"": ""1""," & vbCr & vbLf & "            ""workphone"": ""1""," & vbCr & vbLf & "            ""email"": ""borg@homail.com"",  " & vbCr & vbLf & "            ""municipality"": ""1""," & vbCr & vbLf & "            ""region"": ""1""," & vbCr & vbLf & "            ""country"": ""1""," & vbCr & vbLf & "            ""city"": ""1""," & vbCr & vbLf & "            ""caregiver_address"": ""1""," & vbCr & vbLf & "            ""caregiver_phone"": ""1""," & vbCr & vbLf & "            ""caregiver_mobile"": ""1""," & vbCr & vbLf & "            ""patient_homecare"": ""0""," & vbCr & vbLf & "            ""patient_helpfor"": """"," & vbCr & vbLf & "            ""patient_homecare_phone"": """"," & vbCr & vbLf & "            ""patient_treatment"": ""0""," & vbCr & vbLf & "            ""patient_pigs"": ""0""," & vbCr & vbLf & "            ""patient_mrsa_positive"": ""0""," & vbCr & vbLf & "            ""patient_allergic"": ""0""," & vbCr & vbLf & "            ""patient_allergic_to"": """"," & vbCr & vbLf & "            ""patient_allergic_symptoms"": """"," & vbCr & vbLf & "            ""patient_height"": ""1""," & vbCr & vbLf & "            ""patient_weight"": ""1""," & vbCr & vbLf & "            ""patient_smoke"": ""0""," & vbCr & vbLf & "            ""patient_smoke_amount"": """"," & vbCr & vbLf & "            ""patient_alcohol"": ""0""," & vbCr & vbLf & "            ""patient_alcohol_amount"": """"," & vbCr & vbLf & "            ""patient_medication"": ""0""," & vbCr & vbLf & "            ""patient_medication_amount"": """"," & vbCr & vbLf & "            ""patient_diseases"": ""0""," & vbCr & vbLf & "            ""patient_diseases_type"": """"," & vbCr & vbLf & "            ""patient_pacemaker"": ""0""," & vbCr & vbLf & "            ""gp_address"": ""1""," & vbCr & vbLf & "            ""gp_city"": ""1""," & vbCr & vbLf & "            ""gp_phone"": ""1""," & vbCr & vbLf & "            ""hospital_address"": ""1"",      " & vbCr & vbLf & "            ""hospital_city"": ""1""," & vbCr & vbLf & "            ""may_obtain_information"": ""0""," & vbCr & vbLf & "            ""journal"": ""1""," & vbCr & vbLf & "            ""completed"": ""1""," & vbCr & vbLf & "            ""patient_image"": ""http://voresside.kundeside.dk/media/1012/e-dynamicwebnet-patientformphvardecom-media-_basque-country.png""" & vbCr & vbLf & "        }," & vbCr & vbLf & "        {" & vbCr & vbLf & "            ""name"": ""Steen Sommer""," & vbCr & vbLf & "            ""reg_nr"": ""11111""," & vbCr & vbLf & "            ""cpr_nr"": ""110259-1719""," & vbCr & vbLf & "            ""address"": ""Troldhøjvænget 33""," & vbCr & vbLf & "            ""phone"": ""40281767""," & vbCr & vbLf & "            ""mobile"": ""40281767""," & vbCr & vbLf & "            ""workphone"": ""76950100""," & vbCr & vbLf & "            ""email"": ""steensommer@gmail.com"",  " & vbCr & vbLf & "            ""municipality"": ""Varde""," & vbCr & vbLf & "            ""region"": ""Syddanmark""," & vbCr & vbLf & "            ""country"": ""Danmark""," & vbCr & vbLf & "            ""city"": ""Varde""," & vbCr & vbLf & "            ""caregiver_address"": """"," & vbCr & vbLf & "            ""caregiver_phone"": """"," & vbCr & vbLf & "            ""caregiver_mobile"": """"," & vbCr & vbLf & "            ""patient_homecare"": ""0""," & vbCr & vbLf & "            ""patient_helpfor"": """"," & vbCr & vbLf & "            ""patient_homecare_phone"": """"," & vbCr & vbLf & "            ""patient_treatment"": ""0""," & vbCr & vbLf & "            ""patient_pigs"": ""0""," & vbCr & vbLf & "            ""patient_mrsa_positive"": ""0""," & vbCr & vbLf & "            ""patient_allergic"": ""0""," & vbCr & vbLf & "            ""patient_allergic_to"": """"," & vbCr & vbLf & "            ""patient_allergic_symptoms"": """"," & vbCr & vbLf & "            ""patient_height"": """"," & vbCr & vbLf & "            ""patient_weight"": """"," & vbCr & vbLf & "            ""patient_smoke"": ""0""," & vbCr & vbLf & "            ""patient_smoke_amount"": """"," & vbCr & vbLf & "            ""patient_alcohol"": ""0""," & vbCr & vbLf & "            ""patient_alcohol_amount"": """"," & vbCr & vbLf & "            ""patient_medication"": ""0""," & vbCr & vbLf & "            ""patient_medication_amount"": """"," & vbCr & vbLf & "            ""patient_diseases"": ""0""," & vbCr & vbLf & "            ""patient_diseases_type"": """"," & vbCr & vbLf & "            ""patient_pacemaker"": ""0""," & vbCr & vbLf & "            ""gp_address"": """"," & vbCr & vbLf & "            ""gp_city"": """"," & vbCr & vbLf & "            ""gp_phone"": """"," & vbCr & vbLf & "            ""hospital_address"": """",      " & vbCr & vbLf & "            ""hospital_city"": """"," & vbCr & vbLf & "            ""may_obtain_information"": ""0""," & vbCr & vbLf & "            ""journal"": ""0""," & vbCr & vbLf & "            ""completed"": ""0""," & vbCr & vbLf & "            ""patient_image"": ""http://voresside.kundeside.dk/media/1012/e-dynamicwebnet-patientformphvardecom-media-_basque-country.png""" & vbCr & vbLf & "        }]"
        Dim oa As Object() = DirectCast(Json.Deserialize(jsonstr), Object())
        For Each o As Object In oa
            Dim o2 As IDictionary(Of String, Object) = DirectCast(o, IDictionary(Of String, Object))
            TxtFornavn.Text = o2("name")
            'TxtEfternavn.Text = o2("name")
            TxtHCV.Text = o2("reg_nr")
            TxtCPR.Text = o2("cpr_nr")
            TxtAdresse.Text = o2("address")
            TxtPostBy.Text = o2("city")
        Next
    End Sub

    Private Sub FrmPatientFormular_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        HentJSon()
    End Sub
End Class
Avatar billede steensommer Praktikant
01. september 2015 - 15:36 #10
I et forsøg på at kombinere ovenstående har jeg forsøgt med dette. Jeg får fejl ved:
Dim JResults As JObjects .....
Jeg kan se at "rawresp" indeholder alle data


Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader

        Try
            request = DirectCast(WebRequest.Create("http://voresside.kundeside.dk/json?key=t6%sG39"), HttpWebRequest)

            response = DirectCast(request.GetResponse(), HttpWebResponse)
            reader = New StreamReader(response.GetResponseStream())

            Dim rawresp As String
            rawresp = reader.ReadToEnd()

            Dim jResults As JObject = JObject.Parse(rawresp)
            TxtFornavn.Text = If(jResults("name") Is Nothing, "", jResults("name").ToString())
            TxtAdresse.Text = If(jResults("address") Is Nothing, "", jResults("address").ToString())

        Catch ex As System.Net.WebException
            MsgBox(ex.ToString)
        Finally

        End Try
Avatar billede arne_v Ekspert
02. september 2015 - 02:40 #11
E er bare valgt helt tilfaeldigt (E for Eksperten.dk).

Klassen Json er bar een convenience class.
Avatar billede arne_v Ekspert
02. september 2015 - 02:42 #12
Med et variabelt antal elementer og en loekke skal du enten tilfoeje tekst felter dynamisk *eller* kopiere data over i en List<X) og saa bruge den som DataSource for en avanceret kontrol.
Avatar billede steensommer Praktikant
02. september 2015 - 12:28 #13
Jeg har svært ved at komme videre:
Jeg får følgende fejl ved: "Dim JResults As JObjects .."

An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll

Additional information: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 2, position 2.


Her er koden med den rigtige URL:
  Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader

        request = DirectCast(WebRequest.Create("http://phvarde.kundeside.dk/json?key=t6%$SVAKsG39"), HttpWebRequest)

        response = DirectCast(request.GetResponse(), HttpWebResponse)
        reader = New StreamReader(response.GetResponseStream())

        Dim rawresp As String
        rawresp = reader.ReadToEnd()

        Dim jResults As Object = JObject.Parse(rawresp)
        TxtFornavn.Text = If(jResults("name") Is Nothing, "", jResults("name").ToString())
        TxtAdresse.Text = If(jResults("address") Is Nothing, "", jResults("address").ToString())
Avatar billede steensommer Praktikant
02. september 2015 - 18:41 #14
Jeg har i stedet hentet JSON fra en fil med samme indhold som i ovenstående URL. Det giver samme resultat.
Når jeg sletter den ENE record så fungerer følgende kode:

MEN koden skal jo også kunne fungere med flere records :-)


  Dim jsonstr As String = File.ReadAllText("c:\temp\json.json")

            Dim jResults As JObject = JObject.Parse(jsonstr)
            Dim results As Generic.List(Of JToken) = jResults.Children().ToList()

            For Each item As JProperty In results
                item.CreateReader()
                Select Case item.Name
                    Case "name"
                        TxtFornavn.Text = item.Value.ToString
                    Case "address"
                        TxtAdresse.Text = item.Value.ToString
                    Case "cpr_nr"
                        TxtCPR.Text = item.Value.ToString
                    Case "reg_nr"
                        TxtHCV.Text = item.Value.ToString
                    Case "phone"
                        TxtTelefon.Text = item.Value.ToString
                    Case "mobile"
                        TxtMobil.Text = item.Value.ToString
                    Case "email"
                        TxtEmail.Text = item.Value.ToString
                    Case "workphone"
                        TxtArbejdstelefon.Text = item.Value.ToString
                    Case "municipality"
                        TxtKommune.Text = item.Value.ToString
                    Case "region"
                        TxtRegion.Text = item.Value.ToString
                    Case "country"
                        TxtLand.Text = item.Value.ToString
                    Case "city"
                        TxtPostBy.Text = item.Value.ToString
                End Select
            Next
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester