后台java的字符串,如:
String s = "[{\"property\":\"id\",\"direction\":\"ASC\"}]";我想把s先转化为json对象后,再通过属性来求property和direction的值,
由于s不是标准的json对象的字符串,前后多了[],所以,我用下面的方面,转不正确
JSONObject a = JSONObject.fromObject(s); 当然,应该可以把[]去掉后再转,但我感觉似乎还有其它更好的办法,一时想不起来,不知道,各位高人,有何建议?或者这样说吧,我怎么样才可以快速的找出s中的property的值为id,direction的值为ASC呢?
String s = "[{\"property\":\"id\",\"direction\":\"ASC\"}]";我想把s先转化为json对象后,再通过属性来求property和direction的值,
由于s不是标准的json对象的字符串,前后多了[],所以,我用下面的方面,转不正确
JSONObject a = JSONObject.fromObject(s); 当然,应该可以把[]去掉后再转,但我感觉似乎还有其它更好的办法,一时想不起来,不知道,各位高人,有何建议?或者这样说吧,我怎么样才可以快速的找出s中的property的值为id,direction的值为ASC呢?
string a=serializer.Serialize(d);这个不知道成不
public String getValue(String key, String src) {
int index = src.indexOf("\""+key+"\":\"");
if (index<0) return null;
String sub = src.substring(index+key.length()+4);
return src.substring(0,sub.indexOf("\"");
}String s = "[{\"property\":\"id\",\"direction\":\"ASC\"}]";
String property = getValue("property", s);
String direction = getValue("direction", s);
但是,却出错了,不知道为什么
看看这个
可以新建一个类:
public class A{
private String property;
private String direction;
// get set省略
}然后把s中的值转入到A中去,然后在json这个A