版权声明:如若转载,请联系作者。 https://blog.csdn.net/liu16659/article/details/84444795
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"));
}
}
执行结果如下:
参考文章