Link to home
Start Free TrialLog in
Avatar of Jeff Collins
Jeff CollinsFlag for Australia

asked on

Best way to parse out a json string in VB6?

I'm currently calling a web service that is providing a json feed using VB6. What is the best way to parse the information. I currently have the string being returned which I am dumping to a rich text box and formatting for test purposes, but would like to know what the best recommended way of parsing the variable names, and data out.

Is it best to simple runs through and parsing it as a string, using pointers and counters etc... to determine the various sections, or is there a pre-written function somewhere that makes the task slightly less cumbersome? Any thoughts would be good, not after a complete guide.

A sample of the receiving string is as follows:
{
  "section":[
    {
      "client_id":"1124290",
      "m_code_4char":"JCOL",
      "venue":"Johnny Collingworth",
      "updated_ts":null,
      "invoices":[
        {
          "no":100,
          "description":"Fees - Jan",
          "value":300.50,
          "reduction":0,
          "updated_ts":null
        },
        {
          "no":101,
          "description":"Fees - Feb",
          "value":450.00,
          "reduction":0,
          "updated_ts":null
        },
        {
          "no":102,
          "description":"Fees - Mar",
          "value":125.00,
          "reduction":0,
          "updated_ts":null
        },
        {
          "no":103,
          "description":"Fees - Apr",
          "value":87.50,
          "reduction":0,
          "updated_ts":null
        },
        {
          "no":104,
          "description":"Fees - May",
          "value":786.00,
          "reduction":0,
          "updated_ts":null
        },
      ],
      "client_ReductionFactor":0
    },
    {
      "client_id":"1124291",
      "m_code_4char":"JSMI",
      "venue":"Johnny Smith",
      "updated_ts":null,
      "invoices":[
        {
          "no":105,
          "description":"Fees - Jan",
          "value":300.50,
          "reduction":0,
          "updated_ts":null
        },
        {
          "no":106,
          "description":"Fees - Feb",
          "value":450.00,
          "reduction":0,
          "updated_ts":null
        },
        {
          "no":107,
          "description":"Fees - Mar",
          "value":125.00,
          "reduction":0,
          "updated_ts":null
        },
        {
          "no":108,
          "description":"Fees - Apr",
          "value":87.50,
          "reduction":0,
          "updated_ts":null
        },
        {
          "no":109,
          "description":"Fees - May",
          "value":786.00,
          "reduction":0,
          "updated_ts":null
        },
      ],

      "creditnotes":[
        {
          "no":10035,
          "description":"Fees - Jan",
          "value":-75.00,
          "updated_ts":null
        },
        {
          "no":10036,
          "description":"Fees - Feb",
          "value":-45,
          "updated_ts":null
        },
      ],
      "client_ReductionFactor":0
    },
  ],
  "feed_version":1.09,
  "server_ts":0
}

Open in new window

Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Pls try this..

install-package Newtonsoft.json
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Dim jsonString As String = getresponseFromtheServer
    Dim jsonObject As JObject = JObject.Parse(jsonString)
    Dim ListItems As List(Of JToken) = jsonObject.Children().ToList
    
	Dim outputCols AS String = ""
	
    For Each items As JProperty In ListItems
        
		item.CreateReader()
		
        Select Case items.Name
            Case "Pawan"
                outputCols += "Pawan:" + vbCrLf
				
                For Each Pawan As JObject In items.Values
                    
					Dim u As String = Pawan("Details")
                    ....................
					
                Next

        End Select
    Next

Open in new window



Also look at this blog post -  

http://www.newtonsoft.com/json/help/html/Introduction.htm
Avatar of Jeff Collins

ASKER

Thanks Pawan,

This would be for the .NET environment wouldn't it? The software I'm modifying is classic VB6.
No the code is for VB6. Pls Try it out.
Thanks, I'll try now.
SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial