我现在后台有一个String 型的 message ,怎样将其转换为jsonObject 然后传到前台,具体是怎样转换,急啊!
展开全部
var data="{'name':'jack'}";
var json = eval("("+data+")")
alert(json.name);
是不是这个?
或者是这个?
public static String toJSON(Object obj) {
HashMap<String, String> map = new HashMap<String, String>();
Class c = obj.getClass();
Field[] fields = c.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
String name = fields[i].getName();
try {fields[i].setAccessible(true);
Object o = fields[i].get(obj);
if (o instanceof Number) {
map.put("\"" + name + "\"", o.toString());
} else if (o instanceof String) {
map.put("\"" + name + "\"", "\"" + o.toString() + "\"");
}
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
}
String s = map.toString();
String str = s.replaceAll("\"=", "\":");
return str;
}
var json = eval("("+data+")")
alert(json.name);
是不是这个?
或者是这个?
public static String toJSON(Object obj) {
HashMap<String, String> map = new HashMap<String, String>();
Class c = obj.getClass();
Field[] fields = c.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
String name = fields[i].getName();
try {fields[i].setAccessible(true);
Object o = fields[i].get(obj);
if (o instanceof Number) {
map.put("\"" + name + "\"", o.toString());
} else if (o instanceof String) {
map.put("\"" + name + "\"", "\"" + o.toString() + "\"");
}
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
}
String s = map.toString();
String str = s.replaceAll("\"=", "\":");
return str;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询