日期:2014-05-20  浏览次数:20648 次

求助,java用httpclient包,模拟发送json和接受json。
我网上搜索了代码,一般发送json,都是用键值对的,例如:
 JSONObject obj = new JSONObject();  

 obj.put("name", "your name");  
 obj.put("parentId", "your parentid");  
 httppost.setEntity(new StringEntity(obj.toString()));  


但是我现在要发送抓包的结果,:
{header:{"code": -100, "message": {"title": "", "detail": ""}},body:{dataStores:{},parameters:{"args": ["menu_jw_pj_xspj_x", ">>"], "responseParam": "tooltip"}}}

这么长的json数据,能不能直接发送json的字符串?
求解,如果可以的话,代码如何写?

------解决方案--------------------
httppost.setEntity(new StringEntity(obj.toString()));

这不就已经准备完毕了,直接就可以执行了。。。还需要干啥?
------解决方案--------------------
还是没十分理解你的最终目的,不过我猜测是对嵌套关系存疑虑。

如果你的内容没有嵌套关系,就全都是ojb.put(key, value);

如果有嵌套关系,就是 subObj=new JSONObject();obj.put(key, subObj);

如果需要数组,就用JSONArray 对象。


那么你需要的是:{header:{"code": -100, "message": {"title": "", "detail": ""}},body:{dataStores:{},parameters:{"args": ["menu_jw_pj_xspj_x", ">>"], "responseParam": "tooltip"}}}

这里面是有嵌套关系的,你先将其格式化,然后就知道嵌套关系了:
JScript code

{
    header : {
        "code" : -100, "message" : {
            "title" : "", "detail" : ""
        }
    }, body : {
        dataStores : {}, parameters : {
            "args" : [ "menu_jw_pj_xspj_x", ">>" ], "responseParam" : "tooltip"
        }
    }
};