FastJson 数据读取练习

首先导入依赖

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.56</version>
		</dependency>

练习

package com.amat.fastjson;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
public class JsonPath {
	public static void main(String[] args) {
	    String jsonStr = "{\n" +
                "    \"store\": {\n" +
                "        \"bicycle\": {\n" +
                "            \"color\": \"red\",\n" +
                "            \"price\": 19.95\n" +
                "        },\n" +
                "        \"book\": [\n" +
                "            {\n" +
                "                \"author\": \"刘慈欣\",\n" +
                "                \"price\": 8.95,\n" +
                "                \"category\": \"科幻\",\n" +
                "                \"title\": \"三体\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"author\": \"itguang\",\n" +
                "                \"price\": 12.99,\n" +
                "                \"category\": \"编程语言\",\n" +
                "                \"title\": \"go语言实战\"\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "}";
	    System.out.println(jsonStr);
	    //第一本书title
        String title = (String) JSONPath.read(jsonStr, "store.book[0].title");
        System.out.println(title);
        
        List<String> titles =  (List<String>) JSONPath.read(jsonStr, "store.book.title");
        System.out.println(titles);
        
        List<Double> prices =  (List<Double>) JSONPath.read(jsonStr, "$.store.book.price");
        System.out.println(prices);
        
        //多层结构
        List<String> prices2 =  (List<String>) JSONPath.read(jsonStr, "..price");
        System.out.println(prices2);
        
        String str="1:2";
        System.out.println(JSON.isValid(str));

	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值