String text = {"base_resp":{"ret":10702,"err_msg":"can not send this type of msg"}}
JSON.parseObject(text, MsgJson.class)
MsgJson class code is as follows , taken out of the ret is always 0, err_msg be null. ask where wrong ?
public class MsgJson {
BaseResp base_resp = new BaseResp();
class BaseResp {
private int ret;
private String err_msg;
public int getRet() {
return ret;
}
public void setRet(int ret) {
this.ret = ret;
}
public String getMsg() {
return err_msg;
}
public void setMsg(String err_msg) {
this.err_msg = err_msg;
}
}
} ------ Solution ------------------------------------- -------
public String getMsg () {
return err_msg;
}
public void setMsg (String err_msg) {
this.err_msg = err_msg;
}
you see your name these two methods .
------ For reference only -------------------------------------- -
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonUtils {
public static String encodeList (Object [] objs) {
return encodeList (Arrays.asList (objs));
}
@ SuppressWarnings ("unchecked")
public static String encodeList (Collection objs) {
JSONArray list = new JSONArray ();
if (objs == null | | objs.size () == 0)
return list.toString ();
for (Object ae: objs) {
list.add (ae);
}
return list.toString ();
}
@ SuppressWarnings ("unchecked")
public static String encodeObject (Object obj) {
if (obj instanceof Collection)
return encodeList ((Collection) obj);
JSONObject jo = JSONObject.fromObject (obj);
return jo.toString ();
}
@ SuppressWarnings ("unchecked")
public static
if (str == null | | "". equals (str))
return null;
JSONArray obj = JSONArray.fromObject (str);
Object [] rts = obj.toArray ();
List
for (int i = 0; i
T ele = (T) JSONObject.toBean ((JSONObject) jo, clazz);
result.add (ele);
}
return result;
}
@ SuppressWarnings ("unchecked")
public static
JSONObject jsonObject = JSONObject.fromObject (json);
T bean = (T) JSONObject.toBean (jsonObject, clz);
return bean;
}
}
------ For reference only ---------------------------------- -----
JSON.parseObject (text, MsgJson.class) see you this way who knows where the wrong
------ For reference only - -------------------------------------
inherent in this approach is Json class the method is as follows:
public static final <T> T parseObject(String text, Class<T> clazz) {
/* 321 */ return parseObject(text, clazz, new Feature[0]);
/* */ } ------ For reference only ----------------------------------- ----
I think it should be MsgJson class structure written wrong, but I do not know how to change ! ! !
------ For reference only ---------------------------------------
top, although not in a hurry problems, but this project, a lot of places need to use this stuff , hoping to thoroughly understand it. Seeking advice ! !
------ For reference only ---------------------------------------
What you see the wrong place , you write this class only ret, and err_msg this property
parse this should be no problem .
{"ret": 10702, "err_msg": "can not send this type of msg"}
If you want to parse the string {"base_resp": {"ret": 10702, "err_msg": "can not send this type of msg"}} you want to write a class
------ For reference only ---------------------------------- -----
public class A {
private BaseResp b;
written after SET and get methods ;
}
------ For reference only --------------------------------- ------
MsgJson inside the class sets up a BaseResp class ah !
BaseResp class is to parse {"ret": 10702, "err_msg": "can not send this type of msg"} 's .
entire MsgJson class of analytic {"base_resp": {"ret": 10702, "err_msg": "can not send this type of msg"}}. ( Do not know if it 's forehead (⊙ o ⊙) ...)
------ For reference only ----------------------- ----------------
JSON.parseObject (text, MAP.class) For me, I transferred directly into MAP, very easy to use
------ For reference only -------------------------------------- -
give a simple and complete example I refer to reference pictures. I always shining examples do not own what !
------ For reference only ---------------------------------------
saw you what's wrong ; that you wrote this class only ret, and err_msg this property
parse this should be no problem .
{"ret": 10702, "err_msg": "can not send this type of msg"}
You can first try to resolve this . Each name-value pairs corresponding BEAN in a Zodiac .
------ For reference only ---------------------------------- -----
I just tried , ret can parse it out too 10702, err_msg or null oh.
------ For reference only -------------------------------------- -
public class A {
private BaseResp base_resp;
written after SET and get methods ;
}
write ah I give you the class methods , you see.
------ For reference only -------------------------------------- -
you find the key , and indeed these two methods were not written . You know what works? I always thought that the method name willingly take it !
------ For reference only -------------------------------------- -
you find the key , and indeed these two methods were not written . You know what works? I always thought that the method name willingly take it !
ah resolved with the use of tools might be used to generate reflection mechanism .
------ For reference only ------------------------------------ ---
If you learn this , you're the first to write various types of Bean, you can also put BEAN into collections , or collections .
look after what turned into JSON structure , so you can know what kind of json able to turn into what kind of bean or collection classes.
------ For reference only -------------------------------------- -
Bean? never contacted . I will try to learn this one . Thank you for your help , oh. Quack. . .
------ For reference only -------------------------------------- -
Bean? never contacted . I will try to learn this one . Thank you for your help , oh. Quack. . . you write a class that javaBean, I give you the class can look good, oh oh Do not thank me I was to come and collect points quack
------ For reference only --------------------------------------- < br> Oh, this is a question and answer , and finally solve the problem .
will MsgJson class modified as follows:
public class MsgJson {
private BaseResp base_resp = new BaseResp();
public BaseResp getBase_resp(){
return base_resp;
}
public void setBase_resp(BaseResp base_resp){
this.base_resp = base_resp;
}
class BaseResp {
private int ret;
private String err_msg;
public int getRet() {
return ret;
}
public void setRet(int ret) {
this.ret = ret;
}
public String getErr_msg() {
return err_msg;
}
public void setErr_msg(String err_msg) {
this.err_msg = err_msg;
}
}
}
thanks u012463264 help , O (∩ _ ∩) O haha ~
没有评论:
发表评论