Format and indent Json string in vb6

117 views Asked by At

I just have this string:

dim stringa as string

stringa="{"data":{"GET:oauth.openapi.it\/counters":{"counter":22,"paid":0,"limit":false},"GET:oauth.openapi.it\/scopes":{"counter":6,"paid":0,"limit":false},"POST:oauth.openapi.it\/token":{"counter":1,"paid":0,"limit":false},"GET:imprese.openapi.it\/advance":{"counter":14,"paid":0,"limit":false},"GET:imprese.openapi.it\/base":{"counter":2,"paid":0,"limit":false}},"success":true,"message":"","error":null}"

It is all on only one line.

Is it possible to indent and have to the end the typical Json format, similar to:

{
"data":{
    "GET:oauth.openapi.it/counters":{
        "counter":22,
        "paid":0,
        "limit":false
    },
    "GET:oauth.openapi.it/scopes":{
        "counter":6,
        "paid":0,
        "limit":false
    },
    "POST:oauth.openapi.it/token":{
        "counter":1,
        "paid":0,
        "limit":false
    },
    "GET:imprese.openapi.it/advance":{
        "counter":14,
        "paid":0,
        "limit":false
    },
    "GET:imprese.openapi.it/base":{
        "counter":2,
        "paid":0,
        "limit":false
    }
},
"success":true,
"message":"",
"error":null
}

In this case I can see better the arrangement of the nodes.

1

There are 1 answers

3
wqw On

You can use JsonDump function like this:

Option Explicit

Private Sub Form_Load()
    Dim stringa As String
    stringa = "{""data"":{""GET:oauth.openapi.it\/counters"":{""counter"":22,""paid"":0,""limit"":false},""GET:oauth.openapi.it\/scopes"":{""counter"":6,""paid"":0,""limit"":false},""POST:oauth.openapi.it\/token"":{""counter"":1,""paid"":0,""limit"":false},""GET:imprese.openapi.it\/advance"":{""counter"":14,""paid"":0,""limit"":false},""GET:imprese.openapi.it\/base"":{""counter"":2,""paid"":0,""limit"":false}},""success"":true,""message"":"""",""error"":null}"
    
    Dim oJson As Object
    Set oJson = JsonParseObject(stringa)
    Debug.Print JsonDump(oJson, MaxWidth:=0)
End Sub

Immediate Window

{
    "data": {
        "GET:oauth.openapi.it/counters": {
            "counter": 22,
            "paid": 0,
            "limit": false
        },
        "GET:oauth.openapi.it/scopes": {
            "counter": 6,
            "paid": 0,
            "limit": false
        },
        "POST:oauth.openapi.it/token": {
            "counter": 1,
            "paid": 0,
            "limit": false
        },
        "GET:imprese.openapi.it/advance": {
            "counter": 14,
            "paid": 0,
            "limit": false
        },
        "GET:imprese.openapi.it/base": {
            "counter": 2,
            "paid": 0,
            "limit": false
        }
    },
    "success": true,
    "message": "",
    "error": null
}

You'll need mdJson.bas included in your project first.

There are other parameters to JsonDump besides MaxWidth which affect output too.