Java 使用XStream将JSON转换为HashMap

Java 使用XStream将JSON转换为HashMap,java,json,hashmap,converter,xstream,Java,Json,Hashmap,Converter,Xstream,我有一个映射形式的JSON字符串: { “露西”:{ “id”:456, “全名”:“古柏,安吉拉”, “用户id”:“2733245678”, “stin”:“2733212346” }, “玛拉”:{ “id”:123, “全名”:“鲍勃,史蒂夫”, “用户id”:“abc213”, “stin”:“9040923411” } } 我想将其转换为HashMap,键为“Lucy”、“Myra”等,值为JavaObject Class Person { String id; String ful

我有一个映射形式的JSON字符串:

{ “露西”:{ “id”:456, “全名”:“古柏,安吉拉”, “用户id”:“2733245678”, “stin”:“2733212346” }, “玛拉”:{ “id”:123, “全名”:“鲍勃,史蒂夫”, “用户id”:“abc213”, “stin”:“9040923411” } }

我想将其转换为HashMap,键为“Lucy”、“Myra”等,值为JavaObject

Class Person
{
String id;
String fullName;
String userId;
Strring stin;

}
我该怎么做?我有一个提示,我需要使用MapConverter,但是关于如何使用它的文档很少。我已经使用XStream创建了列表(使用addImplicitCollection),但不知道Map。

您也可以使用“jackson”来实现这一点

ObjectMapper objMapper=new ObjectMapper();
Map-mpCards=objMapper.readValue(strJSONString,new-TypeReference(){});

将JSON转换为地图地图。遍历外部映射,获取映射项的值(内部映射),并将其传递给对象的构造函数,该构造函数接受映射并构造相应的对象。将指向内部映射的指针替换为指向构造对象的指针。(为了让Java泛型满意,您可能需要创建一个新映射,而不是更新旧映射中的值。)

伪代码(几乎可以在任何地方):

Map theWholeThing=myJSONParser.parseJSONIntoObject(theWholeJSONString);
映射newthing=newhashmap();
//使用你最喜欢的地图迭代风格——我随机选择了这个
for(Map.Entry:theWholeThing.entrySet()){
String key=entry.getKey();
Map value=entry.getValue();
人员=新人员(价值);
新的。放(钥匙,人);
}
归还新的东西;
课堂人员:

Person(Map<String, String) map) {
    this.id = map.get("id");
    this.fullName = map.get("full_name");
    this.userId = map.get("user_id");
    this.stin = map.get("stin");
}

Person(你能用示例代码详细说明一下吗?我无法修改JSON结构(无法控制!)。谢谢,我最终使用了Jackson,因为我不知道如何使用XStream进行此操作。我想知道您是否知道在使用JacksonIgnore节点进行解析时如何忽略节点意味着,您想跳过Person类中的某些属性?您能让我知道详细信息吗?
Map<String, Map> theWholeThing = myJSONParser.parseJSONIntoObject(theWholeJSONString);
Map<String, Person> theNewThing = new HashMap<String, Person>();
// Use your favorite Map iterating style -- I picked this at random
for (Map.Entry<String, String> entry : theWholeThing.entrySet()) {
    String key = entry.getKey();
    Map<String, String> value = entry.getValue();
    Person person = new Person(value);
    theNewThing.put(key, person);
}
return theNewThing;
Person(Map<String, String) map) {
    this.id = map.get("id");
    this.fullName = map.get("full_name");
    this.userId = map.get("user_id");
    this.stin = map.get("stin");
}