Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 迭代ResponseBody中的项,并将它们放入HashMap Spring启动中_Json_Mongodb_Spring Boot_Spring Restcontroller - Fatal编程技术网

Json 迭代ResponseBody中的项,并将它们放入HashMap Spring启动中

Json 迭代ResponseBody中的项,并将它们放入HashMap Spring启动中,json,mongodb,spring-boot,spring-restcontroller,Json,Mongodb,Spring Boot,Spring Restcontroller,在SpringBoot中的REST控制器中,我尝试迭代RequestBody响应中的值,并将其中一些值放入POST端点中的HashMap中 我发送的JSON的结构如下: {"name":"yogurt","vitaminA":6,"vitaminb12":5} 到目前为止,端点如下所示: @RequestMapping("/create") public NutrientList createNUtrientList(@RequestBody NutrientList nutrientList

在SpringBoot中的REST控制器中,我尝试迭代RequestBody响应中的值,并将其中一些值放入POST端点中的HashMap中

我发送的JSON的结构如下:

{"name":"yogurt","vitaminA":6,"vitaminb12":5}
到目前为止,端点如下所示:

@RequestMapping("/create")
public NutrientList createNUtrientList(@RequestBody NutrientList nutrientList) {
    Map<String, Double> nutrientMap = new HashMap<String,Double>();
    //get nutrient values, need help with this part
    for()
    //add values to map
    NutrientList nl = new NutrientList(nutrientList.getName(), nutrientMap);
    //will save to repository
    return nl;
}
@RequestMapping(“/create”)
public NutrientList createNUtrientList(@RequestBody NutrientList NutrientList){
Map nutrientMap=新HashMap();
//获取营养价值,需要这部分的帮助吗
for()
//向地图添加值
NutrientList nl=新的NutrientList(NutrientList.getName(),nutrientMap);
//将保存到存储库
返回nl;
}
NutrientList类如下所示:

public class NutrientList {
    @Id
    private ObjectId id;
    @JsonProperty("name")
    private String name;
    @JsonProperty("nutrientMap")
    Map <String,Double> nutrientMap = new HashMap<String,Double>();

    public NutrientList() {}

    public NutrientList(String name, Map<String, Double> nutrientMap) {
        this.id = new ObjectId();
        this.name = name;
        this.nutrientMap = nutrientMap;
    }
    //setters and getters
}
公共类NutrientList{
@身份证
私有ObjectId;
@JsonProperty(“名称”)
私有字符串名称;
@JsonProperty(“nutrientMap”)
Map nutrientMap=新HashMap();
公共NutrientList(){}
公共NutrientList(字符串名称,映射nutrientMap){
this.id=新的ObjectId();
this.name=名称;
this.nutrientMap=nutrientMap;
}
//二传手和接球手
}
数据由单独的营养素存储在数据库中,而不是地图。我看到NutrientList类不共享相同的结构,但是有没有什么方法可以绕过它,在不改变地图在数据库中存储方式的情况下使用地图

我需要使用一张地图,因为有很多营养素,我不想让它们有单独的变量。非常感谢你。如果有什么不清楚,请告诉我

编辑:
我也可以将数据库中的数据从csv转换成JSON格式,但我还没有找到在线工具来提供这种灵活性

如果您有一个有效密钥列表,可以使用以下命令:

private static final List validKeys=Arrays.asList(“vitaminA”、“vitaminB”/*.*/);
@请求映射(“/create”)
public NutrientList createNutrientList(@RequestBody映射RequestBody){
Map nutrientMap=新HashMap();
for(字符串:requestBody.keySet()){
if(validKeys.contains(营养素)和&requestBody.get(营养素)instanceof Number){
Number=(Number)requestBody.get(营养素);
nutrientMap.put(nutrient,number.doubleValue());
}
}
String name=(String)requestBody.get(“name”);//也许可以检查name是否存在,并且是否真的是一个字符串
返回新的NutrientList(名称、nutrientMap);
}
如果您想使用Java 8流API,可以尝试:

private static final List validKeys=Arrays.asList(“vitaminA”、“vitaminB”/*.*/);
@请求映射(“/create”)
public NutrientList createNutrientList(@RequestBody映射RequestBody){
Map nutrientMap=requestBody.entrySet().stream()
.filter(e->validKeys.contains(e.getKey()))
.filter(e->e.getValue()实例数字)
.collect(Collectors.toMap(Map.Entry::getKey,e->)((Number)e.getValue()).doubleValue());
字符串名称=可选的.ofNullable(requestBody.get(“名称”))
.filter(字符串的n->n实例)
.map(n->(字符串)n)
.orelsetrow(IllegalArgumentException::new);
返回新的NutrientList(名称、nutrientMap);
}

希望这能有所帮助。

可能重复感谢资源,但我不尝试循环映射,而是将响应正文放入映射。您确定请求正文是NutrientList吗?它似乎与您展示的Json字符串不同。是的,它是不同的,但是有什么方法可以解决这个问题吗?我可以将参数更改为什么?