Jayway - Json-Path 的使用(二)
最编程
2024-07-18 10:20:14
...
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Iterator;
import java.util.List;
import com.jayway.jsonpath.JsonPath;
public class TestJsonPath {
public static void main(String[] args) {
String sjson = readtxt();
print("--------------------------------------getJsonValue--------------------------------------");
getJsonValue(sjson);
}
private static String readtxt() {
StringBuilder sb = new StringBuilder();
try {
FileReader fr = new FileReader("D:/books.txt");
BufferedReader bfd = new BufferedReader(fr);
String s = "";
while((s=bfd.readLine())!=null) {
sb.append(s);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(sb.toString());
return sb.toString();
}
private static void getJsonValue(String json) {
//The authors of all books:获取json中store下book下的所有author值
List<String> authors1 = JsonPath.read(json, "$.store.book[*].author");
//All authors:获取所有json中所有author的值
List<String> authors2 = JsonPath.read(json, "$..author");
//All things, both books and bicycles
//authors3返回的是net.minidev.json.JSONArray:获取json中store下的所有value值,不包含key,如key有两个,book和bicycle
List<Object> authors3 = JsonPath.read(json, "$.store.*");
//The price of everything:获取json中store下所有price的值
List<Object> authors4 = JsonPath.read(json, "$.store..price");
//The third book:获取json中book数组的第3个值
List<Object> authors5 = JsonPath.read(json, "$..book[2]");
//The first two books:获取json中book数组的第1和第2两个个值
List<Object> authors6 = JsonPath.read(json, "$..book[0,1]");
//All books from index 0 (inclusive) until index 2 (exclusive):获取json中book数组的前两个区间值
List<Object> authors7 = JsonPath.read(json, "$..book[:2]");
//All books from index 1 (inclusive) until index 2 (exclusive):获取json中book数组的第2个值
List<Object> authors8 = JsonPath.read(json, "$..book[1:2]");
//Last two books:获取json中book数组的最后两个值
List<Object> authors9 = JsonPath.read(json, "$..book[-2:]");
//Book number two from tail:获取json中book数组的第3个到最后一个的区间值
List<Object> authors10 = JsonPath.read(json, "$..book[2:]");
//All books with an ISBN number:获取json中book数组中包含isbn的所有值
List<Object> authors11 = JsonPath.read(json, "$..book[?(@.isbn)]");
//All books in store cheaper than 10:获取json中book数组中price<10的所有值
List<Object> authors12 = JsonPath.read(json, "$.store.book[?(@.price < 10)]");
//All books in store that are not "expensive":获取json中book数组中price<=expensive的所有值
List<Object> authors13 = JsonPath.read(json, "$..book[?(@.price <= $['expensive'])]");
//All books matching regex (ignore case):获取json中book数组中的作者以REES结尾的所有值(REES不区分大小写)
List<Object> authors14 = JsonPath.read(json, "$..book[?(@.author =~ /.*REES/i)]");
//Give me every thing:逐层列出json中的所有值,层级由外到内
List<Object> authors15 = JsonPath.read(json, "$..*");
//The number of books:获取json中book数组的长度
List<Object> authors16 = JsonPath.read(json, "$..book.length()");
print("**********authors1**********");
print(authors1);
print("**********authors2**********");
print(authors2);
print("**********authors3**********");
printOb(authors3);
print("**********authors4**********");
printOb(authors4);
print("**********authors5**********");
printOb(authors5);
print("**********authors6**********");
printOb(authors6);
print("**********authors7**********");
printOb(authors7);
print("**********authors8**********");
printOb(authors8);
print("**********authors9**********");
printOb(authors9);
print("**********authors10**********");
printOb(authors10);
print("**********authors11**********");
printOb(authors11);
print("**********authors12**********");
printOb(authors12);
print("**********authors13**********");
printOb(authors13);
print("**********authors14**********");
printOb(authors14);
print("**********authors15**********");
printOb(authors15);
print("**********authors16**********");
printOb(authors16);
}
private static void print(List<String> list) {
for(Iterator<String> it = list.iterator();it.hasNext();) {
System.out.println(it.next());
}
}
private static void printOb(List<Object> list) {
for(Iterator<Object> it = list.iterator();it.hasNext();) {
System.out.println(it.next());
}
}
private static void print(String s) {
System.out.println("\n"+s);
}
}
输出
{"store":{"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"JavaWeb","author":"X-rapido","title":"Top-link","isbn":"0-553-211231-3","price":32.68},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],"bicycle":{"color":"red","price":19.95}},"expensive":10}
--------------------------------------getJsonValue--------------------------------------
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/workSpaces/SupportPackge/MavenRepository/org/apache/logging/log4j/log4j-slf4j-impl/2.0.2/log4j-slf4j-impl-2.0.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/workSpaces/SupportPackge/MavenRepository/org/slf4j/slf4j-log4j12/1.7.10/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
**********authors1**********
Nigel Rees
Evelyn Waugh
Herman Melville
X-rapido
J. R. R. Tolkien
**********authors2**********
Nigel Rees
Evelyn Waugh
Herman Melville
X-rapido
J. R. R. Tolkien
**********authors3**********
[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"JavaWeb","author":"X-rapido","title":"Top-link","isbn":"0-553-211231-3","price":32.68},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]
{color=red, price=19.95}
**********authors4**********
8.95
12.99
8.99
32.68
22.99
19.95
**********authors5**********
{category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99}
**********authors6**********
{category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95}
{category=fiction, author=Evelyn Waugh, title=Sword of Honour, price=12.99}
**********authors7**********
{category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95}
{category=fiction, author=Evelyn Waugh, title=Sword of Honour, price=12.99}
**********authors8**********
{category=fiction, author=Evelyn Waugh, title=Sword of Honour, price=12.99}
**********authors9**********
{category=JavaWeb, author=X-rapido, title=Top-link, isbn=0-553-211231-3, price=32.68}
{category=fiction, author=J. R. R. Tolkien, title=The Lord of the Rings, isbn=0-395-19395-8, price=22.99}
**********
上一篇: MySQL 主从复制与多主多从部署流程 - 2023.04
下一篇: 微信小程序]全局样式、本地样式、全局配置
推荐阅读
-
Qt/C++编写的mqtt调试助手使用说明--二、特点
-
14-傅里叶变换的代码实现-一、numpy实现傅里叶变换和逆傅里叶变换 1.numpy实现傅里叶变换numpy.fft.fft2实现傅里叶变换,返回一个复数数组(complex ndarray),也就是频谱图像numpy.fft.fftshift将零频率分量移到频谱中心(将左上角的低频区域,移到中心位置) 20*np.log(np.abs(fshift))设置频谱的范围。可以理解为,之前通过傅里叶变换得到复数的数组,是不能通过图像的方法展示出来的,需要转换为灰度图像(映射到[0,255]区间)需要注意的是1> 傅里叶得到低频、高频信息,针对低频、高频处理能够实现不同的目的2> 傅里叶过程是可逆的,图像经过傅里叶变换、逆傅里叶变换后,能够恢复到原始图像3> 在频域对图像进行处理,在频域的处理会反映在逆变换图像上 # 将绘制的图显示在窗口 %matplotlib qt5 import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r"image\lena.bmp",cv2.IMREAD_GRAYSCALE) # 傅里叶变换 f = np.fft.fft2(img) # 移动中心位置 fshift = np.fft.fftshift(f) # 调整值范围 result = 20*np.log(np.abs(fshift)) plt.subplot(1,2,1) plt.imshow(img,cmap=plt.cm.gray) plt.title("original") plt.axis("off") plt.subplot(1,2,2) plt.imshow(result,cmap=plt.cm.gray) plt.title("result") plt.axis("off") plt.show 傅里叶变换的频谱图像: 2.numpy实现逆傅里叶变换numpy.fft.ifft2实现逆傅里叶变换,返回一个复数数组(complex ndarray)numpy.fft.ifftshiftfftshift函数的逆函数,将中心位置的低频,重新移到左上角iimg = np.abs(逆傅里叶变化结果)设置值的范围,映射到[0,255]区间 # 将绘制的图显示在窗口 %matplotlib qt5 import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r"image\boat.bmp",cv2.IMREAD_GRAYSCALE) # 傅里叶变换 f = np.fft.fft2(img) fshift = np.fft.fftshift(f) # 逆傅里叶变换 ishift = np.fft.ifftshift(fshift) iimg = np.fft.ifft2(ishift) iimg = np.abs(iimg) plt.subplot(1,2,1) plt.imshow(img,cmap=plt.cm.gray) plt.title("original") plt.axis("off") plt.subplot(1,2,2) plt.imshow(iimg,cmap=plt.cm.gray) plt.title("iimg") plt.axis("off") plt.show 将一副图像,进行傅里叶变换和逆傅里叶变换后,进行对比(一样的) 实例:通过numpy实现高通滤波,保留图像的边缘信息 获取图像的形状rows,cols = img.shape获取图像的中心点crow,ccol = int(rows/2),int(cols/2)将频谱图像的中心区域(低频区域)设置为0(黑色)fshift[crow-30:crow+30,ccol-30:ccol+30] = 0 # 将绘制的图显示在窗口 %matplotlib qt5 import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r"image\boat.bmp",cv2.IMREAD_GRAYSCALE) # 傅里叶变换 f = np.fft.fft2(img) fshift = np.fft.fftshift(f) # 高通滤波 rows,cols = img.shape crow,ccol = int(rows/2),int(cols/2) fshift[crow-30:crow+30,ccol-30:ccol+30] = 0 # 逆傅里叶变换 ishift = np.fft.ifftshift(fshift) iimg = np.fft.ifft2(ishift) iimg = np.abs(iimg) plt.subplot(1,2,1) plt.imshow(img,cmap=plt.cm.gray) plt.title("original") plt.axis("off") plt.subplot(1,2,2) plt.imshow(iimg,cmap=plt.cm.gray) plt.title("iimg") plt.axis("off") plt.show 使用numpy实现高通滤波的实验结果: 二、opencv实现傅里叶变换和逆傅里叶变换 1.opencv实现傅里叶变换 返回结果 = cv2.dft(原始图像,转换标识)1> 返回结果:是双通道的,第一个通道是结果的实数部分,第二个通道是结果的虚数部分2> 原始图像:输入图像要首先转换成np.float32(img)格式3> 转换标识:flags = cv2.DFT_COMPLEX_OUTPUT,输出一个复数阵列numpy.fft.fftshift将零频率分量移到频谱中心(将左上角的低频区域,移到中心位置)调整频谱的范围,将上面频谱图像的复数数组,转换为可以显示的灰度图像(映射到[0,255]区间)返回值 = 20*np.log(cv2.magnitude(参数1,参数2))1> 参数1:浮点型X坐标值,也就是实部2> 参数2:浮点型Y坐标值,也就是虚部 # 将绘制的图显示在窗口 %matplotlib qt5 import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r"image\lena.bmp",cv2.IMREAD_GRAYSCALE) # 傅里叶变换 dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT) # 移动中心位置 dftShift = np.fft.fftshift(dft) # 调整频谱的范围 result = 20*np.log(cv2.magnitude(dftShift[:,:,0],dftShift[:,:,1])) plt.subplot(1,2,1) plt.imshow(img,cmap=plt.cm.gray) plt.title("original") plt.axis("off") plt.subplot(1,2,2) plt.imshow(result,cmap=plt.cm.gray) plt.title("result") plt.axis("off") plt.show 傅里叶变换的频谱图像: 2.opencv实现逆傅里叶变换返回结果 = cv2.idft(原始数据)1> 返回结果:取决于原始数据的类型和大小2> 原始数据:实数或者复数均可numpy.fft.ifftshiftfftshift函数的逆函数,将中心位置的低频,重新移到左上角调整频谱的范围,映射到[0,255]区间返回值 = cv2.magnitude(参数1,参数2)1> 参数1:浮点型X坐标值,也就是实部2> 参数2:浮点型Y坐标值,也就是虚部 # 将绘制的图显示在窗口 %matplotlib qt5 import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(r"image\lena.bmp",cv2.IMREAD_GRAYSCALE) # 傅里叶变换 dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT) dftShift = np.fft.fftshift(dft) # 逆傅里叶变换 ishift = np.fft.ifftshift(dftShift) iimg = cv2.idft(ishift) iimg = cv2.magnitude(iimg[:,:,0],iimg[:,:,1]) plt.subplot(1,2,1) plt.imshow(img,cmap=plt.cm.gray) plt.title("original") plt.axis("off") plt.subplot(1,2,2) plt.imshow(iimg,cmap=plt.cm.gray) plt.title("inverse") plt.axis("off") plt.show 将一副图像,进行傅里叶变换和逆傅里叶变换后,进行对比(一样的) 实例:通过opencv实现低通滤波,模糊一副图像
-
MATLAB函数fft2在进行二维傅里叶变换时需要注意的要点(包括二维傅里叶变换的物理意义、使用im2double、fftshift、abs和imshow函数)
-
深入理解Linux内核调试技术——kprobe的使用和实现(第二部分)
-
使用MATLAB的solve函数解决复杂的二元高次方程问题
-
使用Matlab进行二元二次拟合的教程
-
简单易懂的C/C++图书管理系统(使用二叉平衡树)
-
Allegro中切割/挖空铜皮步骤 及 该功能无法正常使用时的解决方案-二、切割/挖空铜皮工具功能异常解决方案
-
使用R语言实现工具变量和两阶段最小二乘法的数据生成方法
-
优化 [.NET] Log4net FileAppender 和 MinimalLock 性能问题的解决方案(第二部分:使用 MSMQ)