json資料格式說明

2021-09-07 20:40:55 字數 2033 閱讀 6996

json檔案由物件(集合)、陣列、key/value元素組成,可以相互巢狀。

使用大括號包圍的是物件,使用中括號包圍的是陣列,冒號分隔的是元素。

元素的key只能是字串。

元素的value資料型別可以是:

物件、陣列容器中每個元素之間使用逗號隔開,容器的最後乙個元素不加逗號

頂級物件都是匿名的,也就是沒有key

下面是乙個json格式資料的示例:

,

"published":true,

"label":,

"nextpost":null,

"comments":[,]

}

用注釋分析這個json:

,

"published":true, # 文章是否發布,布林型別

"label":, # 文章標籤,沒有給標籤,所以空陣列},]

}

一般來說,json格式轉換成語言中的資料結構時,有以下幾個比較通用的規則(只是比較普通的方式,並非一定):

例如,上面的示例,轉換成go中的資料結構時,得到的結果如下:

// 使用名稱a代替頂層的匿名物件

type a struct `json:"label"`

nextpost inte***ce{} `json:"nextpost"`

comments comment `json:"comments"`

}type author struct

type comment struct

比如轉換成python中的資料時,得到的結果如下:

from typing import list, any

class author:

id: int

name: str

def __init__(self, id: int, name: str) -> none:

self.id = id

self.name = name

class comment:

id: int

content: str

author: str

def __init__(self, id: int, content: str, author: str) -> none:

self.id = id

self.content = content

self.author = author

# 使用了名稱a代替頂層的匿名物件

class a:

id: int

content: str

author: author

published: bool

label: list[any]

next_post: none

comments: list[comment]

def __init__(self, id: int, content: str, author: author, published: bool, label: list[any], next_post: none, comments: list[comment]) -> none:

self.id = id

self.content = content

self.author = author

self.published = published

self.label = label

self.next_post = next_post

self.comments = comments

quicktype工具,可以輕鬆地將json檔案轉換成各種語言對應的資料結構。

在vscode中有相關外掛程式

先在命令面板中輸入"set quicktype target language"選擇要將json轉換成什麼語言的資料結構

再輸入"open quicktype for json"就可以將當前json檔案轉換對應的資料結構。

Json資料格式

在web 系統開發中,經常會碰到客戶端和伺服器端互動的問題,比如說客戶端傳送乙個 ajax 請求,然後在伺服器端進行計算,計算後返回結果,客戶端接收到這個響應結果並對它進行處理。那麼這個結果以一種什麼資料結構返回,客戶端才能比較容易和較好的處理呢?通過幾個專案的實踐,我發現 json 格式的資料是一...

JSON資料格式

下面這段文字,摘錄自留作備忘 21世紀初,douglas crockford尋找一種簡便的資料交換格式,能夠在伺服器之間交換資料。當時通用的資料交換語言是xml,但是douglas crockford覺得xml的生成和解析都太麻煩,所以他提出了一種簡化格式,也就是json。json的規格非常簡單,只...

json資料格式

json是一種與語言無關的資料交換的格式,作用 使用ajax進行前後臺資料交換 移動端與服務端的資料交換。1.物件格式 例如 user物件 或者 2.陣列 集合形式 例如 list用json資料格式表示 總結 1.物件格式和資料格式可以相互巢狀 例如 var json 取name 建寧 alert ...