Java JSON处理

Java JSON处理,java,json,spring,jackson,Java,Json,Spring,Jackson,我很难用Java处理下面的JSON,它是从外部Ansible playbook返回的: {"Sample": { "tag_description":"abc","tag_category_id":"def","tag_id":"ghi" }, "Sample1": { "tag_description":"jkl","tag_category_id":"mno","tag_id":"pqr" } } 我已经能够使用自定

我很难用Java处理下面的JSON,它是从外部Ansible playbook返回的:

{"Sample":
    {
        "tag_description":"abc","tag_category_id":"def","tag_id":"ghi"
    },
    "Sample1":
    {
        "tag_description":"jkl","tag_category_id":"mno","tag_id":"pqr"
    }
 }
我已经能够使用自定义反序列化程序成功解析JSON的一个部分,尽管它只得到第一个部分。任何想法都会受到极大的赞赏

@JsonComponent
public class TagSerializer extends JsonDeserializer<Tag> {



@Override
public Tag deserialize(JsonParser jsonParser,
                        DeserializationContext deserializationContext) throws IOException,
        JsonProcessingException {

    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = mapper.getFactory();
    JsonNode treeNode = jsonParser.getCodec().readTree(jsonParser);

    Iterator<Map.Entry<String, JsonNode>> fields = treeNode.fields();
    String name = "";

    // collect the tag name
    Map.Entry<String, JsonNode> entry = fields.next();
    name = entry.getKey();

    // now that we have the tag name, parse it as a separate JSON object
    JsonNode node = entry.getValue();

    // get the values from the JSON
    String description = node.get("tag_description").asText();
    String category_id = node.get("tag_category_id").asText();
    String tag_id = node.get("tag_id").asText();

     return new Tag(name, category_id, description, tag_id);
}

这不是一个直接的答案,而是一个可能方向的指南,使用Gson

package test;
import java.util.Map;
import com.google.gson.Gson;
public class JsonTest {
    public static void main(final String... args) {
        new JsonTest().run();
    }
    public void run() {
        final Gson gson = new Gson();
        final Map<?, ?> result = gson.fromJson("{" + 
                "    \"Sample\": {" + 
                "        \"tag_description\": \"abc\"," + 
                "        \"tag_category_id\": \"def\"," + 
                "        \"tag_id\": \"ghi\"" + 
                "    }," + 
                "    \"Sample1\": {" + 
                "        \"tag_description\": \"jkl\"," + 
                "        \"tag_category_id\": \"mno\"," + 
                "        \"tag_id\": \"pqr\"" + 
                "    }" + 
                "}", Map.class);
        System.out.println("Map size: " + result.size());
    }
}
封装测试;
导入java.util.Map;
导入com.google.gson.gson;
公共类JsonTest{
公共静态void main(最终字符串…参数){
新建JsonTest().run();
}
公开募捐{
最终Gson Gson=新Gson();
最终映射结果=gson.fromJson(“{”+
“\”示例\“:{”+
“\”标记描述\“:\”abc\“,“+
“\”标记\类别\ id\“:\”定义\“,“+
“\“标记id\”:\“ghi\”+
"    }," + 
“\”样本1\“:{”+
“\”标记描述\“:\”jkl\“,“+
“\”标记\类别\ id\“:\”mno\“,“+
“\“标记id\”:\“pqr\”+
"    }" + 
“}”,Map.class);
System.out.println(“映射大小:+result.size());
}
}

结果大小为2。映射条目是键控的Sample、Sample1,值是包含节点的列表。你可以用调试器看到这个。

如果你正在使用Spring MVC,考虑在引用到<代码> @ RealStests< /C>时显式声明所需的类型,并让框架为你做< /P>的工作。
@PostMapping(value="/store", consumes = APPLICATION_JSON_VALUE)
public void tagJson(@RequestBody Map<String, Tag> json) {
  // Do not mess with ObjectMapper here, Spring will do the thing for you
}
@PostMapping(value=“/store”,consumes=APPLICATION\u JSON\u value)
public void tagJson(@RequestBody-Map-json){
//不要在这里乱搞ObjectMapper,Spring会帮你做的
}

太棒了,这非常有效!使用它,我可以简单地设置标签上返回的字符串名称,并保存到JPA存储库中。谢谢你!非常感谢您的回复!Spring框架最终承担了很多繁重的工作,但我肯定会看看GsonSpring包含了Jackson JSON处理器,这相当于Gson。
package test;
import java.util.Map;
import com.google.gson.Gson;
public class JsonTest {
    public static void main(final String... args) {
        new JsonTest().run();
    }
    public void run() {
        final Gson gson = new Gson();
        final Map<?, ?> result = gson.fromJson("{" + 
                "    \"Sample\": {" + 
                "        \"tag_description\": \"abc\"," + 
                "        \"tag_category_id\": \"def\"," + 
                "        \"tag_id\": \"ghi\"" + 
                "    }," + 
                "    \"Sample1\": {" + 
                "        \"tag_description\": \"jkl\"," + 
                "        \"tag_category_id\": \"mno\"," + 
                "        \"tag_id\": \"pqr\"" + 
                "    }" + 
                "}", Map.class);
        System.out.println("Map size: " + result.size());
    }
}
@PostMapping(value="/store", consumes = APPLICATION_JSON_VALUE)
public void tagJson(@RequestBody Map<String, Tag> json) {
  // Do not mess with ObjectMapper here, Spring will do the thing for you
}