我可以使用SQL查询显示JSON数据吗?
下面的部分是我当前的JSON数据:
[
{
"id":"BKU0000007",
"judul":"jsjsj",
"no_isbn":"ssksk",
"penulis":"jsjsj",
"penerbit":"iieie",
"tahun_terbit":"2011",
"halaman":"202",
"berat":"220",
"cover":"20202",
"dimensi":"0202",
"kategori":"Horor",
"bahasa":"2020",
"stok":"22020",
"gambar":"BKU0000007.jpeg",
"harga":"333",
"sinopsis":"0202022"
},
{
"id":"BKU0000006",
"judul":"sshhs",
"no_isbn":"3i3i3i",
"penulis":"ssiis",
"penerbit":"oo3o3",
"tahun_terbit":"2009",
"halaman":"20",
"berat":"10000",
"cover":"alks",
"dimensi":"20x30",
"kategori":"",
"bahasa":"skks",
"stok":"20",
"gambar":"BKU0000006.jpeg",
"harga":"29999",
"sinopsis":"skkssk"
}
]
[
{
"id":"BKU0000007",
"judul":"jsjsj",
"no_isbn":"ssksk",
"penulis":"jsjsj",
"penerbit":"iieie",
"tahun_terbit":"2011",
"halaman":"202",
"berat":"220",
"cover":"20202",
"dimensi":"0202",
"kategori":"Horor",
"bahasa":"2020",
"stok":"22020",
"gambar":"BKU0000007.jpeg",
"harga":"333",
"sinopsis":"0202022"
},
{
"id":"BKU0000006",
"judul":"sshhs",
"no_isbn":"3i3i3i",
"penulis":"ssiis",
"penerbit":"oo3o3",
"tahun_terbit":"2009",
"halaman":"20",
"berat":"10000",
"cover":"alks",
"dimensi":"20x30",
"kategori":"",
"bahasa":"skks",
"stok":"20",
"gambar":"BKU0000006.jpeg",
"harga":"29999",
"sinopsis":"skkssk"
}
]
我想使用SQL查询生成此数据,例如:
SELECT * FROM data etc ...
我该如何完成这项任务?有人可以指导我完成这个吗?
感谢您花时间和耐心解决此事。
答案 0 :(得分:0)
我可以建议如下:
a)您需要使用ObjectMapper对象
b)调用ObjectMapper对象的writeValueAsString(object)
c)要使用ObjectMapper,你需要在类路径中使用jackson core,jackson annotation,jackson databind jar
示例示例: 我创建了Example类来模拟你的解决方案 - 代码如下:
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
class Example{
int id;
String name;
public Example(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class MainApp {
public static void main(String[] args) throws JsonProcessingException {
List<List<Example>> list=new ArrayList<List<Example>>();
List<Example> exampleList1=new ArrayList<>();
exampleList1.add(new Example(1,"A"));
list.add(exampleList1);
List<Example> exampleList2=new ArrayList<>();
exampleList2.add(new Example(1,"B"));
list.add(exampleList2);
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String writeValueAsString = mapper.writeValueAsString(list);
System.out.println(writeValueAsString);
}
}
输出:
[[{ “ID”:1, “名称”: “A”}],[{ “ID”:1, “名称”: “B”}]]
使用Example类替换属性。
(如果你有dao代码,那么你需要在调用sql查询并获得结果后从dao层准备示例对象列表的列表)
希望你明白了!