gpt4 book ai didi

java - 如何从 json 字符串中删除键名

转载 作者:行者123 更新时间:2023-12-02 09:57:52 24 4
gpt4 key购买 nike

我有一个像这样的 json 字符串。

[
{
"_source": {
"name": "Jam Brong",
"image": "https://asdf.asdf.com/asdf/image/upload/w_30,h_30,c_fill/product/fb7d3dcb505fba76262c0c6383d844ae.jpg",
"price": "2500",
"slug": "133-jam-brong",
"short_name": "Jam Brong"
}
},
{
"_source": {
"name": "Jam abcfdfjn",
"image": "https://asdf.asdf.com/asdf/image/upload/w_30,h_30,c_fill/product/7bbd3d081dd03c442e4cb27321a7b50c.jpg",
"price": "10888",
"slug": "87-jam-abcfdfjn",
"short_name": "Jam abcfdfjn"
}
}
]

我需要删除“_source”:{

这样我就可以得到这样的json字符串。

[
{

"name": "Jam Brong",
"image": "https://asdf.asdf.com/asdf/image/upload/w_30,h_30,c_fill/product/fb7d3dcb505fba76262c0c6383d844ae.jpg",
"price": "2500",
"slug": "133-jam-brong",
"short_name": "Jam Brong"

},
{

"name": "Jam abcfdfjn",
"image": "https://asdf.asdf.com/asdf/image/upload/w_30,h_30,c_fill/product/7bbd3d081dd03c442e4cb27321a7b50c.jpg",
"price": "10888",
"slug": "87-jam-abcfdfjn",
"short_name": "Jam abcfdfjn"

}
]

我尝试使用 replaceAll("("_source:{"),"");此代码将向我显示一些错误,例如 number Expected

我不知道如何对包含 _{ 的字符串使用 regex

在我考虑使用replaceAll之前,我尝试过jackson这样的。

String responses ="";
ObjectNode node = new ObjectMapper().readValue(response.toString(), ObjectNode.class);
ProductList productListInstance = new ProductList();
List<Product> productList = new ArrayList<>();

try {
if(node.get("hits").get("hits").isArray()){
for (final JsonNode objNode : node.get("hits").get("hits")) {
Product products = new ObjectMapper().readValue(objNode.get("_source").toString(), Product.class);
productList.add(products);
}
productListInstance.setProductList(productList);
}
responses = productListInstance.toString();
}
catch (Exception ex){
responses = productListInstance.toString();
}
return responses;

实际上第一个json字符串是这样的:

{
"hits": {
"hits": [
{
"_source": {
"name": "Jam Brong",
"image": "https://asdf.asdf.com/asdf/image/upload/w_30,h_30,c_fill/product/fb7d3dcb505fba76262c0c6383d844ae.jpg",
"price": "2500",
"slug": "133-jam-brong",
"short_name": "Jam Brong"
}
},
{
"_source": {
"name": "Jam abcfdfjn",
"image": "https://asdf.sdf.com/asdf/image/upload/w_30,h_30,c_fill/product/7bbd3d081dd03c442e4cb27321a7b50c.jpg",
"price": "10888",
"slug": "87-jam-abcfdfjn",
"short_name": "Jam abcfdfjn"
}
}
]
}
}

最佳答案

您可以使用 Jackson 来实现此目的。首先,定义一个代表您的数据模型的 bean。

public static class Source {
private String name;
private String image;
private String price;
private String slug;
private String shortName;

@JsonCreator
public Source(@JsonProperty("_source") Map<String, Object> rawJson) {
this.name = rawJson.get("name").toString();
this.image = rawJson.get("image").toString();
this.price = rawJson.get("price").toString();
this.slug = rawJson.get("slug").toString();
this.shortName = rawJson.get("short_name").toString();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}

public String getSlug() {
return slug;
}

public void setSlug(String slug) {
this.slug = slug;
}

public String getShortName() {
return shortName;
}

public void setShortName(String shortName) {
this.shortName = shortName;
}
}

遵守@JsonCreator构造函数上的注释。然后编写序列化和反序列化的代码:

    final ObjectMapper mapper = new ObjectMapper();
Source[] sources = mapper.readValue(jsonStr, Source[].class);
String converted = mapper.writeValueAsString(sources);
System.out.println(converted);

打印:

[
{
"name": "Jam Brong",
"image": "https://asdf.asdf.com/asdf/image/upload/w_30,h_30,c_fill/product/fb7d3dcb505fba76262c0c6383d844ae.jpg",
"price": "2500",
"slug": "133-jam-brong",
"shortName": "Jam Brong"
},
{
"name": "Jam abcfdfjn",
"image": "https://asdf.asdf.com/asdf/image/upload/w_30,h_30,c_fill/product/7bbd3d081dd03c442e4cb27321a7b50c.jpg",
"price": "10888",
"slug": "87-jam-abcfdfjn",
"shortName": "Jam abcfdfjn"
}
]

关于java - 如何从 json 字符串中删除键名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55861222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com