做視頻能賺錢的網(wǎng)站seoheuni
前言
本文簡單介紹toml;并且和json轉(zhuǎn)化做對比,以及我對toml設(shè)計(jì)的理解。
參考:
TOML: 簡體中文 v1.0.0
json和toml轉(zhuǎn)化工具
在線JSON轉(zhuǎn)toml-toml轉(zhuǎn)JSON - bejson在線工具
正文
數(shù)組
說白了,就是一個(gè)變量名,有多個(gè)變量的值。值的類型,可以相同,可以不同。
integers = [ 1, 2, 3 ]
colors = [ "紅", "黃", "綠" ]
nested_array_of_ints = [ [ 1, 2 ], [3, 4, 5] ]
nested_mixed_array = [ [ 1, 2 ], ["a", "b", "c"] ]
string_array = [ "所有的", '字符串', """是相同的""", '''類型''' ]# 允許混合類型的數(shù)組
numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ]
contributors = ["Foo Bar <foo@example.com>",{ name = "Baz Qux", email = "bazqux@example.com", url = "https://example.com/bazqux" }
]
對應(yīng)josn
{"integers": [ 1,2,3 ],"colors": ["紅",黃","綠"],"nested_array_of_ints": [[1,2],[3,4,5]],"nested_mixed_array": [[1,2],["a","b","c"]],"string_array": ["所有的","字符串","是相同的","類型"],"numbers": [0.1,0.2,0.5,1,2,5],"contributors": ["Foo Bar <foo@example.com>",{"name": "Baz Qux","email": "bazqux@example.com","url": "https://example.com/bazqux"}]
}
表
在json中對應(yīng)“對象”;要注意的是json中,對象有的有名字,有的沒有名字。
toml方括號,就說明用的是“表”。也就是json中的對象。
創(chuàng)建表的方法:
#創(chuàng)建了sites對象,內(nèi)容有site1
[sites]
site1 = "www.runoob.com"#創(chuàng)建了“嵌套”對象
[dog]
[dog."tater.man"]
[dog."tater.man".type]
name = "pug"#等價(jià)于上面
dog."tater.man".type.name = "pug"
例子:
{"name":"runoob","alexa":10000,"sites": {"site1":"www.runoob.com","site2":"m.runoob.com","site3":"c.runoob.com"}
}轉(zhuǎn)成toml
name = "runoob"
alexa = 10000
[sites]
site1 = "www.runoob.com"
site2 = "m.runoob.com"
site3 = "c.runoob.com"
嵌套
{"dog": {"tater": {"type": {"name": "pug"}}}
}轉(zhuǎn)成toml
[dog]
[dog.tater]
[dog.tater.type]
name = "pug"
#等價(jià)于上面——沒有括號
dog.tater.type.name = "pug"
demo
[merter1.VoltageSurge]STimeStamp= ["1","2","3"]ETimeStamp= ["4","5","6"][merter1.VoltageDip]STimeStamp= ["1","2","3"]ETimeStamp= ["1","2","3"][merter2.VoltageSurge]STimeStamp= ["1","2","3"]ETimeStamp= ["1","2","3"]
[merter2.VoltageDip]STimeStamp= ["1","2","3"]ETimeStamp= ["1","2","3"]const auto data = toml::parse("C:/Users/45428/Desktop/test.toml");for (const auto& kv : data.as_table()) {const toml::value& value = kv.second;// 遍歷子表const toml::table& sub_table = value.as_table();for (const auto& sub_kv : sub_table) {const toml::key& sub_key = sub_kv.first;const toml::value& sub_value = sub_kv.second;const auto STimeStamp = toml::find<std::vector<string>>(sub_value, "STimeStamp");const auto ETimeStamp = toml::find<std::vector<string>>(sub_value, "ETimeStamp");}}
內(nèi)聯(lián)表
和“表”一樣。
他的存在就是讓文件變得更加緊湊相對于“表”來說。
既然是為了更緊湊,就不允許他換行。
你要內(nèi)聯(lián)表,就得在大括號里定義完,不允許再起一行后面增加。或者用內(nèi)聯(lián)表去增加一般的表。
#表
[name]
first = "Tom"
last = "Preston-Werner"
[point]
x = 1
y = 2
[animal]
type.name = "pug"#內(nèi)聯(lián)表——更緊湊——上下都一樣
name = { first = "Tom", last = "Preston-Werner" }
point = { x = 1, y = 2 }
animal = { type.name = "pug" }
表數(shù)組
就是表里嵌套數(shù)組;
對應(yīng)的json就是對象嵌套數(shù)組。
【【數(shù)組名字】】
例子:
{"products": [#數(shù)組products——兩個(gè)元素{"name": "Hammer","sku": 738594937},{"name": "Nail","sku": 284758393}],"products1": [#數(shù)組products1——一個(gè)元素{"name": "Hammer","sku": 738594937}]
}#數(shù)組products
[[products]]
name = "Hammer"
sku = 738594937
[[products]]
name = "Nail"
sku = 284758393#數(shù)組products1
[[products1]]
name = "Hammer"
sku = 738594937
嵌套
[[fruits]]
name = "apple"[fruits.physical] # 子表
color = "red"
shape = "round"[[fruits.varieties]] # 嵌套表數(shù)組
name = "red delicious"[[fruits.varieties]]
name = "granny smith"[[fruits]]
name = "banana"[[fruits.varieties]]
name = "plantain"{"fruits": [{"name": "apple","physical": {"color": "red","shape": "round"},"varieties": [{"name": "red delicious"},{"name": "granny smith"}]},{"name": "banana","varieties": [{"name": "plantain"}]}]
}
表和表數(shù)組的區(qū)別
【對象名】:toml的表,json的對象
{"a": {}
}
【【對象名】】:toml的表數(shù)組,json的數(shù)組里放對象
{"a": [{}]
}
嵌套:
【a.b】a對象嵌套b對象
{"a": {"b": {}}
}
【【a.b】】a數(shù)組嵌套b對象
[[a.b]]{"a": {"b": [{}]}
}
嵌套的都是對象