21wiki
 Learn, Trade, Earn
Menu

JSON tips and codes

  • Je samodokumentačný, textový, ale je dlhší ako CSV

Typický JSON:
[

{"Code":"SK","Infected":"100","Deaths":"1","Cured":"20"},
{"Code":"CZ","Infected":"200","Deaths":"2","Cured":"25"},
{"Code":"PL","Infected":"300","Deaths":"3","Cured":"26"}

]

Prvý a posledný znak JSON: [ ]

Systém: {"Názov1":"Hodnota1","Názov2":"Hodnota2,...}

Vždy odriadkuj JSONy aby každý riadok bol nový záznam

Iný typ JSON: JSON ako pole

  • Začínaš { } zátvorkami

{

"Code":["Infected", "Deaths", "Cured"]

"SK":[ "100","1","20" ],

"CZ":[ "100","1","20" ]

}

JSON validátor

https://jsonformatter.curiousconcept.com/

Generácia z Excelu pomocou Excel VBA makra

Kód na export hárka do JSON

Public Sub tojson()

    Dim fso As Object

    Set fso = CreateObject("Scripting.FileSystemObject")

    jsonFilename = fso.GetBaseName(ActiveWorkbook.Name) & ".json"

    fullFilePath = Application.ActiveWorkbook.Path & "\" & jsonFilename

    Dim fileStream As Object

    Set fileStream = CreateObject("ADODB.Stream")

    fileStream.Type = 2 'Specify stream type - we want To save text/string data.

    fileStream.Charset = "utf-8" 'Specify charset For the source text data.

    fileStream.Open 'Open the stream And write binary data To the object

    Dim wkb As Workbook

    Set wkb = ThisWorkbook

    Dim wks As Worksheet

    Set wks = wkb.Sheets(1)

    lcolumn = wks.Cells(1, Columns.Count).End(xlToLeft).Column

    lrow = wks.Cells(Rows.Count, "A").End(xlUp).Row

    Dim titles() As String

    ReDim titles(lcolumn)

    For i = 1 To lcolumn

        titles(i) = wks.Cells(1, i)

    Next i

    fileStream.WriteText "["

    dq = """"

    escapedDq = "\"""

    For j = 2 To lrow

        For i = 1 To lcolumn

            If i = 1 Then

                fileStream.WriteText "{"

            End If

            cellvalue = Replace(wks.Cells(j, i), dq, escapedDq)

            fileStream.WriteText dq & titles(i) & dq & ":" & dq & cellvalue & dq

            If i <> lcolumn Then

                fileStream.WriteText ","

            End If

        Next i

        fileStream.WriteText "}"

        If j <> lrow Then

            fileStream.WriteText ","

        End If

    Next j

    fileStream.WriteText "]"

    fileStream.SaveToFile fullFilePath, 2 'Save binary data To disk

    a = MsgBox("Saved to " & fullFilePath, vbOKOnly)

End Sub

About author
robert
Contact
About me
Privacy Policy
Cookies
Copyright © 2021 21wiki.com
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram