Results 1 to 6 of 6

Thread: VB6 JSON Parser

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    97

    VB6 JSON Parser

    I have json string like this:
    Code:
    {"0":"1","id":"1","1":"cliv","name":"cliv","2":"alecsandri","address":"alecsandri","3":"29-Jan-14 10:42:55 AM","datetime":"29-Jan-14 10:42:55 AM"}{"0":"2","id":"2","1":"dsad","name":"dsad","2":"adasdsa dadsa","address":"adasdsa dadsa","3":"29-Jan-14 10:45:03 AM","datetime":"29-Jan-14 10:45:03 AM"}{"0":"3","id":"3","1":"dada","name":"dada","2":"dasdadsa","address":"dasdadsa","3":"29-Jan-14 11:02:44 AM","datetime":"29-Jan-14 11:02:44 AM"}{"0":"4","id":"4","1":"dragcos","name":"dragcos","2":"craiova","address":"craiova","3":"29-Jan-14 11:28:44 AM","datetime":"29-Jan-14 11:28:44 AM"}{"0":"5","id":"5","1":"andrei","name":"andrei","2":"calea bucuresti","address":"calea bucuresti","3":"29-Jan-14 11:56:58 AM","datetime":"29-Jan-14 11:56:58 AM"}
    And i use VB6 JSON Parser Class Library from here:
    HTML Code:
    http://www.ediy.co.nz/vbjson-json-parser-library-in-vb6-xidc55680.html
    and this code to parse:
    Code:
    Private Sub decodeJSON(sData As String)
        Dim p As Object
        Set p = JSON.parse(sData)
        
        If Not (p Is Nothing) Then
            If JSON.GetParserErrors <> "" Then
                MsgBox JSON.GetParserErrors, vbInformation, "JSON Error"
            Else
                'MsgBox = p.Count
                MsgBox "OUT : " & JSON.toString(p)
                MsgBox p.Item("name")
            End If
        Else
            MsgBox "Parse Error"
        End If
    End Sub
    ... but I read only only first row...
    HOW CA I LOOP? (i want to load all record in a recordset...)
    Thank you...
    Last edited by cliv; Jan 29th, 2014 at 07:26 AM.

  2. #2
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: VB6 JSON Parser

    If you want to read all 5 of your "Rows" properly, then you have to validate your JSON-string first:

    Any decent parser will only go up to the point where the second curly-brace-opening starts:
    {"0":"1","id":"1","1":"cliv","name":"cliv","2":"alecsandri","address":"alecsandri","3":"29-Jan-14 10:42:55 AM","datetime":"29-Jan-14 10:42:55 AM"}{...

    You will need to introduce commas between the Curly-Braces, to separate the Object-entries - and you will have to enclose the whole
    construct within square-brackets, to treat those 5-Objects as Array-members ... below is a correctly formatted version of what I mean.
    (you can simply use an Online-JSON-validator for such things - e.g. this one here was quite good: http://jsonlint.com

    Oh - and there's quite a bit of "unnecessary redundancy" in your data - hope you're aware of that.

    Code:
    [
        {
            "0": "1",
            "1": "cliv",
            "2": "alecsandri",
            "3": "29-Jan-14 10:42:55 AM",
            "id": "1",
            "name": "cliv",
            "address": "alecsandri",
            "datetime": "29-Jan-14 10:42:55 AM"
        },
        {
            "0": "2",
            "1": "dsad",
            "2": "adasdsa dadsa",
            "3": "29-Jan-14 10:45:03 AM",
            "id": "2",
            "name": "dsad",
            "address": "adasdsa dadsa",
            "datetime": "29-Jan-14 10:45:03 AM"
        },
        {
            "0": "3",
            "1": "dada",
            "2": "dasdadsa",
            "3": "29-Jan-14 11:02:44 AM",
            "id": "3",
            "name": "dada",
            "address": "dasdadsa",
            "datetime": "29-Jan-14 11:02:44 AM"
        },
        {
            "0": "4",
            "1": "dragcos",
            "2": "craiova",
            "3": "29-Jan-14 11:28:44 AM",
            "id": "4",
            "name": "dragcos",
            "address": "craiova",
            "datetime": "29-Jan-14 11:28:44 AM"
        },
        {
            "0": "5",
            "1": "andrei",
            "2": "calea bucuresti",
            "3": "29-Jan-14 11:56:58 AM",
            "id": "5",
            "name": "andrei",
            "address": "calea bucuresti",
            "datetime": "29-Jan-14 11:56:58 AM"
        }
    ]
    Olaf

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    97

    Re: VB6 JSON Parser

    Quote Originally Posted by Schmidt View Post
    I
    You will need to introduce commas between the Curly-Braces
    Than you for your answer...but this is the string return by php script on server side
    Code:
    $result=mysql_query($sql);
    while($rows=mysql_fetch_array($result)){
    print json_encode($rows);
    ...so i need to read this.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: VB6 JSON Parser

    You have bad JSON - look at this image!!
    Attached Images Attached Images  

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    New Member
    Join Date
    Oct 2021
    Posts
    2

    Re: VB6 JSON Parser

    Hi, sorry to bring the topics again, but I have the proper JSON format, So how can I loop through all the sets. Thank You in Advance.

  6. #6
    New Member
    Join Date
    Oct 2021
    Posts
    2

    Re: VB6 JSON Parser

    Quote Originally Posted by imranctgbd View Post
    Hi, sorry to bring the topics again, but I have the proper JSON format, So how can I loop through all the sets. Thank You in Advance.
    I think I solve the issue. Thank you anyway :-)

    If Not (p Is Nothing) Then
    If JSON.GetParserErrors <> "" Then
    MsgBox JSON.GetParserErrors, vbInformation, "JSON Error"
    Else
    Dim I As Long
    ' MsgBox p.Count
    ' MsgBox "OUT : " & JSON.toString(p)
    ' MsgBox p.Item("name")
    I = 0
    For I = 1 To p.Count
    MsgBox p.Item(I).Item("id") & " - " & p.Item(I).Item("name")
    Next
    End If
    Else
    MsgBox "Parse Error"
    End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width