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

时间:2019-04-26 05:20:53

标签: java json string

我有一个像这样的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"
                }
            }
        ]
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用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"
  }
]