我将一个String类型json格式的字符转换成JSONObject 
public String json = "[\''Url\'':\''http://127.0.0.1/Project/smsAlleywayAction!sendSms.do\'',\''CorpID\'':\''root\'',\''Pwd\'':\''root\'']";方法如下:
public int sendMsg(String phoneNum, String message){

//构造HttpClient的实例
HttpClient httpClient = new HttpClient();

int sendFlag = 0;
Object obj=JSONValue.parse(json);
      JSONArray array=(JSONArray)obj;
      JSONObject o;
     
try {
o = (JSONObject) array.get(0);
this.Url = (String)o.get("Url");
      this.CorpID = (String)o.get("CorpID");
      this.Pwd = (String)o.get("Pwd");
     
} catch (Exception e1) {
e1.printStackTrace();
}不知道为什么Object obj=JSONValue.parse(json);这步obj为null,请问是我的String json格式写错了么?应该怎么写?

解决方案 »

  1.   

    看有木有异常,有的话就是json格式问题,没的话就是json没传过来吧
      

  2.   

    \''Url\''\''  你这是要干什么?应该是
    \"Url\"
      

  3.   

    我写在一个action里了, json可以直接拿来用,内容就是上面那么写的,JSONValue.parse(json);这个方法一运行就null了
      

  4.   

    还有,JSONValue
    你确定这个类中没有错误?自己看看 JSONValue.parse(String) 方法,找找有可能出错的地方。
      

  5.   

    格式我调了下,现在是
    public String json = "{\"Url\":\"http://127.0.0.1/Project/smsAlleywayAction!sendSms.do\",\"CorpID\":\"root\",\"Pwd\":\"root\"}";

    转换到object是
    {"Url":"http:\/\/127.0.0.1\/Project\/smsAlleywayAction!sendSms.do","Pwd":"root","CorpID":"root"}JSONArray array=(JSONArray)obj;时又报错了,报不能转换的错误
    org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray
      

  6.   

    JSONObject jsonObj = new JSONObject(json);
      

  7.   

    JSONObject jsonObj = new JSONObject(json);
    JSONArray materInfo = jsonObj.getJSONArray("result");你json又没数组,干嘛取数组?
      

  8.   

    JSONObject jsonObj = new JSONObject(json);先new对象
      

  9.   

    json-lib可以用下面的
    net.sf.json.JSONObject.fromObject(json)