如何在Spring控制器中读取JSON数组值

时间:2018-11-15 20:35:03

标签: java json spring

我想迭代产品并获取名称,代码和价格的列表,并在我的Model类中进行设置。任何帮助将不胜感激-我该如何迭代。当我使用obj.get(“ Products”)-它只是作为字符串打印-卡住了进行迭代。

{
    "id": "skd3303ll333",
    "Products": [{
            "name": "apple",
            "code": "iphone-393",
            "price": "1939"

        },
        {
            "name": "ipad",
            "code": "ipad-3939",
            "price": "900"

        }
    ]
}


@PostMapping(path="/create", consumes=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> create(@RequestBody  Map<String, Object> obj ) { 
System.out.println("Products :" + obj.get("Products"));
  }

3 个答案:

答案 0 :(得分:2)

有两种方法可以做到这一点,

1)通过类型转换(个人而言,我不希望这样)

List<Map<Object,Object>> productslist = (List<Map<Object, Object>>) obj.get("products");
    for(Map entry: productslist) {
        for(Object s: entry.keySet()) {
            System.out.println(s.toString());
            System.out.println(entry.get(s).toString());

        }
    }

2)直接映射到Model类,对于这种方法,您需要在buildpath中使用Jackson库

@JsonIgnoreProperties(unknown =true)
public class Customer {

@JsonProperty("id")
private String id;
@JsonProperty("products")
private List<Products> products;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public List<Products> getProducts() {
    return products;
}

public void setProducts(List<Products> products) {
    this.products = products;
   }

}
 @JsonIgnoreProperties(unknown =true)
 class Products{
@JsonProperty("name")
private String name;
@JsonProperty("code")
private String code;
@JsonProperty("price")
private String price;

public String getName() {
    return name;
}

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

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getPrice() {
    return price;
}

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

}

控制器

public ResponseEntity<Object> create(@RequestBody  Customer obj ) {

答案 1 :(得分:1)

您正在尝试使用Map<String, Object> obj处理json,这在某种程度上是可行的,但是您最想做的是定义一个或多个POJO类。这些代表json。

public class IdWrapper {
    private String id;
    @JsonProperty("Products")
    private List<Product> products;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<Product> getProducts() {
        return products;
    }
}

public class Product {
    private String name;
    private String code;
    private String price;

    public String getName() {
        return name;
    }

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

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getPrice() {
        return price;
    }

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

在您的控制器中,如下所示:

@RestController
@RequestMapping("test")
public class DemoController {
    @PostMapping()
    public void test(@RequestBody IdWrapper productsWrapper) {
        System.out.println();
    }
}

答案 2 :(得分:1)

您需要具有两个类的POJO结构:

public class Product {
    private String name;
    private String code;
    private int price;
}

public class ProductsGroup {
    private long id;
    private List<Product> products;
    // getters/setters
}

并将您的方法签名更改为:

@PostMapping(path="/create", consumes=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ProductsGroup> create(@RequestBody ProductsGroup productGroup) 
{ 
   System.out.println("Products :" + productGroup.getProducts());
}