在使用第三方api的使用,有时候会从网络中获得json数据,所以说我们将如何解析json数据?
下面小编将通过以下几点来进行json的讲解
1.什么是JSON? (http://www.json.org/)
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
(图片来自:http://www.cnblogs.com/xiaoluo501395377/p/3446605.html)
2.Json数据类型
2-1.json对象
2-2.json数组
ps:JSONObject与JSONArray的区别
3.解析JSON数据(小编使用的GSON进行json数据的解析)
3-1 【JSONObject的解析】
下面是一个json文件:
我们进行解析(解析一部分):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package
cn.edu.bzu.json;
import
java.io.FileNotFoundException;
import
java.io.FileReader;
import
com.google.gson.JsonArray;
import
com.google.gson.JsonIOException;
import
com.google.gson.JsonObject;
import
com.google.gson.JsonParser;
import
com.google.gson.JsonSyntaxException;
public
class
Read {
public
static
void
main(String args[]){
JsonParser parse =
new
JsonParser();
//创建json解析器
try
{
JsonObject json=(JsonObject) parse.parse(
new
FileReader(
"weather.json"
));
//创建jsonObject对象
System.out.println(
"resultcode:"
+json.get(
"resultcode"
).getAsInt());
//将json数据转为为int型的数据
System.out.println(
"reason:"
+json.get(
"reason"
).getAsString());
//将json数据转为为String型的数据
JsonObject result=json.get(
"result"
).getAsJsonObject();
JsonObject today=result.get(
"today"
).getAsJsonObject();
System.out.println(
"temperature:"
+today.get(
"temperature"
).getAsString());
System.out.println(
"weather:"
+today.get(
"weather"
).getAsString());
}
catch
(JsonIOException e) {
e.printStackTrace();
}
catch
(JsonSyntaxException e) {
e.printStackTrace();
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
}
}
|
输出结果:
3-2 【JSONArray的解析】
下面是一个json文件
{ "cat":"it", "language":[ {"id":1,"ide":"eclipse","name":Java}, {"id":2,"ide":"XCode","name":"Swift"}, {"id":3,"ide":"Visual Stdio","name":"C#"} ], "pop":true }
我们进行解析:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package
cn.edu.bzu.json;
import
java.io.FileNotFoundException;
import
java.io.FileReader;
import
com.google.gson.JsonArray;
import
com.google.gson.JsonIOException;
import
com.google.gson.JsonObject;
import
com.google.gson.JsonParser;
import
com.google.gson.JsonSyntaxException;
public
class
ReadJSON {
public
static
void
main(String args[]){
try
{
JsonParser parser=
new
JsonParser();
//创建JSON解析器
JsonObject object=(JsonObject) parser.parse(
new
FileReader(
"test.json"
));
//创建JsonObject对象
System.out.println(
"cat="
+object.get(
"cat"
).getAsString());
//将json数据转为为String型的数据
System.out.println(
"pop="
+object.get(
"pop"
).getAsBoolean());
//将json数据转为为boolean型的数据
JsonArray array=object.get(
"language"
).getAsJsonArray();
//得到为json的数组
for
(
int
i=
0
;i<array.size();i++){
System.out.println(
"---------------"
);
JsonObject subObject=array.get(i).getAsJsonObject();
System.out.println(
"id="
+subObject.get(
"id"
).getAsInt());
System.out.println(
"name="
+subObject.get(
"name"
).getAsString());
System.out.println(
"ide="
+subObject.get(
"ide"
).getAsString());
}
}
catch
(JsonIOException e) {
e.printStackTrace();
}
catch
(JsonSyntaxException e) {
e.printStackTrace();
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
}
}
|
输出结果:
3-3 【分析】
我们通过Gson进行解析,所以在使用前需要导入Gson.jar
解析json数据时,
1.需要进行创建Gson解析器
2.创建JSONObject对象
3.将json数据转为为相应的数据