Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight data-interexchange format. Prior to JSON, XML was the most commonly used data inter-exchange format. XML still has its dominance. But JSON is also gaining up because of its simplicity. JSON represents the data as string:value pairs, where the value can be string, numbers, arrays, boolean values or even null value.

Take for example

1. string: number

{
 "rollnumber": 1
}

2. string: string

{
 "sitename": "programming"
}

3. string: boolean value

{
 "student" : true
}

4. string: array

[js]
{
"tags" : [
"cpp",
"c",
"PHP"
]
}
[/js]

5. string:object (Note that object is nothing but any of the four representations shown above)

[js]
{
"site": {
"sitename": "programming",
"tags": [
"cpp",
"c",
"PHP"
]
}
}
[/js]

https://linuxprograms.wordpress.com/2010/05/20/introduction-to-json/


这个数据不能正确保存到JSON文件中。因为它是多个JSON对象的集合,而JSON文件只能包含一个JSON对象或一个JSON数组。如果要将这些JSON对象保存到文件中,可以将它们组合成一个JSON数组,然后将数组写入文件中。 以下是一个示例代码,将这些JSON对象组合成一个JSON数组,并将它们写入名为"output.json"的文件中: ```java import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class WriteJsonArrayToFileExample { public static void main(String[] args) { try { FileWriter writer = new FileWriter("output.json"); BufferedWriter buffer = new BufferedWriter(writer); String jsonArray = "[" + "{\"id\":\"1092000\",\"type\":\"reference\",\"title\":\"An Introduction to the History of Mathematics \",\"url\":\"https://archive.org/details/introductiontohi0000eves \"}," + "{\"id\":\"1092000\",\"type\":\"reference\",\"title\":\"数学与算术、算学\",\"url\":\"http://hk.chiculture.net/0803/html/c01/0803c01.html\"}," + "{\"id\":\"1092000\",\"type\":\"reference\",\"title\":\"书评:《毛起来说三角》\",\"url\":\"http://math.ntnu.edu.tw/~horng/letter/vol4no1b.htm\"}," + "{\"id\":\"1092000\",\"type\":\"reference\",\"title\":\"数学的三个发展时期——初等数学时期\",\"url\":\"http://www.pep.com.cn/czsx/xszx/czsxkwyd/czsxkwydsxgs/201008/t20100824_718183.htm\"}" + "]"; buffer.write(jsonArray); buffer.close(); System.out.println("Data has been written to the file."); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } } ``` 此代码将这些JSON对象组合成一个JSON数组,并将它们写入名为"output.json"的文件中。请注意,我们在JSON对象之间添加了逗号",",并将它们组合在一个方括号"[]"中,以表示它们是一个JSON数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值