Java 提取json中的文字
阿新 • • 發佈:2018-12-02
Java 提取json中的文字
1. 簡單的 json 文字
{"result":
{
"code":1,
"msg":"success",
"url":"",
"data":"0",
"date":"1530007139"
},
"data":
{
"token":"iamtoken",
"life":1530005620,
"expires":7200
}
}
2. 需求
如果需要提取code
中的值,該怎麼做呢?如果需要提取token
中的值又該怎麼做呢?需要藉助Alibaba的一個jar 包,其maven依賴如下:
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.53</version>
</dependency>
實現上述需求的程式碼如下:
package utils;
import com.alibaba.fastjson. JSONObject;
public class JsonUtils {
//get a normal string by json string
public static void main(String[] args) {
String content = "{\"result\":{\"code\":1,\"msg\":\"success\",\"url\":\"\",\"data\":\"0\",\"date\":\"1530007139\"},\"data\":{\"token\":\"iamtoken\",\"life\":1530005620,\"expires\":7200}}" ;
System.out.println("token: "+getToken(content));
System.out.println("code: "+getCode(content));
}
public static String getCode(String content) {
JSONObject jsonObject = JSONObject.parseObject(content);
String code = jsonObject.getJSONObject("result").getString("code");
return code;
}
public static String getToken(String content) {
JSONObject jsonObject = JSONObject.parseObject(content);//將該字串解析成jsonObject
String token = jsonObject.getJSONObject("data").getString("token");//提取該物件中的data域,
return token;
}
}
3. 執行結果
4. 複雜的 json
文字
如果 json
文字如下所示,那麼該怎麼獲取呢?
對應的文字如下:
{"data":[
{
"id":524816977,
"type":"answer",
"relationship":
{
"is_author":false,
"is_authorized":false,
"is_nothelp":false,
"is_thanked":false,
"voting":0,
"upvoted_followees":[]
}
},
{
"id":524816978,
"type":"answer",
"relationship":
{
"is_author":true,
"is_authorized":false,
"is_nothelp":false,
"is_thanked":false,
"voting":10,
"upvoted_followees":[]
}
}
]}
可以看到這裡的 json
相當於一個數組了。裡面裝有兩個json
物件,分別是:
{
"id":524816977,
"type":"answer",
"relationship":
{
"is_author":false,
"is_authorized":false,
"is_nothelp":false,
"is_thanked":false,
"voting":0,
"upvoted_followees":[]
}
}
和
{
"id":524816978,
"type":"answer",
"relationship":
{
"is_author":true,
"is_authorized":false,
"is_nothelp":false,
"is_thanked":false,
"voting":10,
"upvoted_followees":[]
}
}
那麼該怎麼獲取這裡面的某個值呢?比如id的值。
如果此時還按照上述簡單json
文字的寫法,程式碼如下:
public void getValue() {
String jsonString = "{\n" +
" \"data\": [\n" +
" {\n" +
" \"id\": 524816977,\n" +
" \"type\": \"answer\",\n" +
" \"relationship\": {\n" +
" \"is_author\": false,\n" +
" \"is_authorized\": false,\n" +
" \"is_nothelp\": false,\n" +
" \"is_thanked\": false,\n" +
" \"voting\": 0,\n" +
" \"upvoted_followees\": []\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": 524816978,\n" +
" \"type\": \"answer\",\n" +
" \"relationship\": {\n" +
" \"is_author\": true,\n" +
" \"is_authorized\": false,\n" +
" \"is_nothelp\": false,\n" +
" \"is_thanked\": false,\n" +
" \"voting\": 10,\n" +
" \"upvoted_followees\": []\n" +
" }\n" +
" }\n" +
" ]\n" +
"}";
JSONObject jsonObject = JSONObject.parseObject(jsonString);
System.out.println("id : "+jsonObject.getJSONObject("data").getJSONObject("id"));
}
執行得到的結果是:
可以看到很明顯的錯誤就是:不能將一個JSONArray
轉換成一個 JSONObject
物件,所以應該修改程式碼成如下的樣子:
public void getValue() {
String jsonString = "{\n" +
" \"data\": [\n" +
" {\n" +
" \"id\": 524816977,\n" +
" \"type\": \"answer\",\n" +
" \"relationship\": {\n" +
" \"is_author\": false,\n" +
" \"is_authorized\": false,\n" +
" \"is_nothelp\": false,\n" +
" \"is_thanked\": false,\n" +
" \"voting\": 0,\n" +
" \"upvoted_followees\": []\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": 524816978,\n" +
" \"type\": \"answer\",\n" +
" \"relationship\": {\n" +
" \"is_author\": true,\n" +
" \"is_authorized\": false,\n" +
" \"is_nothelp\": false,\n" +
" \"is_thanked\": false,\n" +
" \"voting\": 10,\n" +
" \"upvoted_followees\": []\n" +
" }\n" +
" }\n" +
" ]\n" +
"}";
JSONObject jsonObject = JSONObject.parseObject(jsonString);
JSONArray data = jsonObject.getJSONArray("data");
System.out.println("json size is: "+data.size());
//print the
for(int i = 0;i< data.size();i++) {
//輸出其中每一個
System.out.println(data.get(i).toString());
}
for(int i = 0 ;i< data.size();i++) {
System.out.println("id: "+data.getJSONObject(i).getString("id"));
}
}
執行結果如下:
參考文章