是否有任何自定义JSON到java对象解析器,以通过某种配置将json属性映射到Java对象属性。
假设我有课
class Person { String id; String name; String loc;}
我的json String是
{name:"xy",id:12, address: {location: "abc"}}
我需要将json的地图location.address
发送到loc
的{{1}}属性。我们需要通过config XML或prop文件来驱动这个映射。
... EDITED
我正在尝试在属性文件中定义语法或规则 如:
Person
这就是我的想法。不确定如何映射复杂类型,如List,Map等
所以我的代码会调用方法
address.location = loc
id = id
name = name
以这种方式完成实际映射
Person p = ObjectMapper.map(jsonData,Person.class,configuration)
这是我打算做的。任何帮助表示赞赏。 在此先感谢。
答案 0 :(得分:2)
使用以下代码。
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class Test {
public static void main(String... args) throws Exception{
Map<String, String> rules = new HashMap<>();
rules.put("address.location", "loc");
rules.put("name", "name");
rules.put("id", "id");
String jsonData = "{name:'xy',id:'12', address: {location: 'abc'}, phones: ['a', 'b', 'c']}";
Person person = (Person) map(jsonData, Person.class, rules);
System.out.println(new Gson().toJson(person));
}
public static Object map(String jsonData, Class<?> rootType, Map<String, String> rules) throws Exception{
Map<String, Object> genericMap = new HashMap<>();
Type type = new TypeToken<Map<String, Object>>() {}.getType();
genericMap = new Gson().fromJson(jsonData, type);
Object object = rootType.newInstance();
for (Map.Entry<String, String> entry : rules.entrySet()){
String mapKey = entry.getKey();
String mapValue = entry.getValue();
String keys[] = mapKey.split("\\.");
Map<String, Object> obj = null;
if(keys.length > 1){
obj = (Map<String, Object>) genericMap.get(keys[0]);
for(int i=1;i<keys.length-1;i++){
obj = (Map<String, Object>) obj.get(keys[i]);
}
// Method method = object.getClass().getDeclaredMethod("set" + capitalize(mapValue) , String.class);
// method.invoke(object, (String)obj.get(keys[keys.length-1]));
Field field = object.getClass().getDeclaredField(mapValue);
field.set(object, obj.get(keys[keys.length - 1]));
}else{
// Method method = object.getClass().getDeclaredMethod("set" + capitalize(mapValue) , String.class);
// method.invoke(object, (String)genericMap.get(mapValue));
Field field = object.getClass().getDeclaredField(mapValue);
field.set(object, genericMap.get(mapValue));
}
}
return object;
}
public static String capitalize(String line){
return Character.toUpperCase(line.charAt(0)) + line.substring(1);
}
}
class Person {
String id;
String name;
String loc;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
<强>输出强>
{"id":"12","name":"xy","loc":"abc","phones":["a","b","c"]}
<强>详情
答案 1 :(得分:-1)
将此库导入您的项目code.google.com/p/google-gson
之后,可以这样做: (“json”是你要解析的任何json-String。它可能是你在问题中指定的那个。)
Person personFromJson = new Gson().fromJson(json, Person.class);