如何使用 flexjson 将 json 转换为 java Obj?
how to convert json to java Obj using flexjson?
是的。我的代码使用 flexjson 将 java obj 转换为 JSON。经测试没问题。但是我不知道如何将 JSON 转换为 java Obj。有人知道吗?提前感谢您的帮助。
/**
* convert java obj to json using flexjson 2.1.
*
* @param obj
* @return jsonStr
*/
public static String bean2Json(Object obj) {
JSONSerializer serilizer = new JSONSerializer();
return serilizer.serialize(obj);
}
/**
* convert json to java obj using flexjson 2.1.
*
* @param jsonStr
* @param objClass
*
* @return obj
*/
public static <T> T json2Bean(String jsonStr, Class<T> objClass) {
// TODO
}
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
public class DeserializerTest {
public static void main(String[] args)
{
JSONSerializer serilizer = new JSONSerializer();
Apple oneApple = new Apple(123);
String appleString = serilizer.serialize(oneApple);
// Convert Apple String to Apple object here
Apple deserializedApple = new JSONDeserializer<Apple>().deserialize( appleString );
System.out.println("AppleID: "+deserializedApple.appleID);
}
}
public class Apple {
public int appleID;
public Apple(){}
public Apple(int pAppleID){
this.appleID=pAppleID;
}
}
预期的输出应该是 AppleID: 123
希望对您有所帮助
是的。我的代码使用 flexjson 将 java obj 转换为 JSON。经测试没问题。但是我不知道如何将 JSON 转换为 java Obj。有人知道吗?提前感谢您的帮助。
/**
* convert java obj to json using flexjson 2.1.
*
* @param obj
* @return jsonStr
*/
public static String bean2Json(Object obj) {
JSONSerializer serilizer = new JSONSerializer();
return serilizer.serialize(obj);
}
/**
* convert json to java obj using flexjson 2.1.
*
* @param jsonStr
* @param objClass
*
* @return obj
*/
public static <T> T json2Bean(String jsonStr, Class<T> objClass) {
// TODO
}
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
public class DeserializerTest {
public static void main(String[] args)
{
JSONSerializer serilizer = new JSONSerializer();
Apple oneApple = new Apple(123);
String appleString = serilizer.serialize(oneApple);
// Convert Apple String to Apple object here
Apple deserializedApple = new JSONDeserializer<Apple>().deserialize( appleString );
System.out.println("AppleID: "+deserializedApple.appleID);
}
}
public class Apple {
public int appleID;
public Apple(){}
public Apple(int pAppleID){
this.appleID=pAppleID;
}
}
预期的输出应该是 AppleID: 123
希望对您有所帮助