首页 新闻 会员 周边 捐助

Spring mvc怎么把json丢到页面上去,而页面怎么去接后端的json

0
悬赏园豆:180 [已解决问题] 解决于 2016-08-20 17:26

//@Controller
//@RequestMapping("/test")
//public class test {

 
  @ResponseBody @RequestMapping(
"/page") public String test() throws IOException{ //参数jsonStr是内容,path接口url地址 ַ String path="http://ssj.xxxxxxxxxx.com.cn:11003/SXJPData.api/FPNewsApi/homeNewsList?token=0"; byte[] data =jsonStr.getBytes(); String msg = "";// 保存调用http服务后的响应信息 URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(5 * 1000);// 设置连接超时时间 conn.setReadTimeout(20 * 10000);// 设置读取超时时间 // 使用 URL 连接进行输出,则将 DoOutput标志设置为 true conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); // conn.setRequestProperty("Content-Encoding","gzip"); conn.setRequestProperty("Content-Length", String.valueOf(data.length)); OutputStream outStream = conn.getOutputStream();// 返回写入到此连接的输出流 outStream.write(data); outStream.close();// 关闭流 // 保存调用http服务后的响应信息 if (conn.getResponseCode() == 200) { // HTTP服务端返回的编码是UTF-8,故必须设置为UTF-8,保持编码统一,否则会出现中文乱码 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); msg = in.readLine(); in.close(); } conn.disconnect();// 断开连接 ObjectMapper mapper = new ObjectMapper(); JsonNode node = null; node = mapper.readTree(msg); String node2=node.get("result").get(0).get("topNewsList").toString(); JsonNode list = null; list = mapper.readTree(node2); List<FPNewsHome> fpList=null; for (JsonNode p : list) { JsonNode id=p.get("id"); JsonNode title = p.get("title"); JsonNode summary= p.get("summary"); JsonNode author=p.get("author"); JsonNode collectCount=p.get("collectCount"); } System.out.println(list); subObject=array.get(i).getAsJsonObject(); System.out.println(msg); return "test2"; }

上面是我的java代码、在网上复制别人的,自己稍微改动一下。从别人的接口是可以成功获取到json数据的。

 

下面这段json是我成功获取的。

--------------------------------------------------------------------

{"code":1000,"msg":"获取成功","result":[{"subscribe":{"id":"39C94D32D7384DABA4766C30EF441C15","icon":"http://ssj.xxxx.com.cn:11002/thumbnail.aspx?id=1524E3F012AC4D8282A283D6CCB0BF73.png","selectIcon":"http://ssj.xxxxxx.com.cn:11002/thumbnail.aspx?id=1524E3F012AC4D8282A283D6CCB0BF73.png","label":"时尚","logo":"http://ssj.xxxxxx.com.cn:11002/thumbnail.aspx?id=1524E3F012AC4D8282A283D6CCB0BF73.png","newsCount":5,"subscibeCount":0,"isSubscrible":false},"topNewsList":[{"id":"604E85FB6EB941788C923BBCA62C0571","title":"轮播图怎么设置?","summary":"","author":"sa","collectCount":1,"commentCount":0,"image":"http://ssj.xxxxxx.com.cn:11002/thumbnail.aspx?id=A967826AD23D4F33925F51667AF9B1DF.png","sendDate":"2016-08-19 00:00:00","messageFrom":0}],"recommendNewsList":[{"id":"8AA034EDAB8145ED939448DDE7659D36","title":"这是测试","summary":"","author":"sa","collectCount":0,"commentCount":1,"image":"http://ssj.xxxx.com.cn:11002/thumbnail.aspx?id=3B4E8DBE7CBA406FB7E9FAE549E8A8E2.png","sendDate":"2016-08-18 00:00:00","messageFrom":0}],"moreNewsList":[{"id":"3DC9FC367F374C16A0DE806CFAF48C4C","title":"再测试一个咯!","summary":"","author":"sa","collectCount":0,"commentCount":0,"image":"http://ssj.xxxxxx.com.cn:11002/thumbnail.aspx?id=EE2036F3D9364ED9AD0C88AFB0F641AC.png","sendDate":"2016-08-19 00:00:00","messageFrom":0},{"id":"68916E123D25472F8066ABD9AE0C3CB2","title":"不要再测试了%#","summary":"","author":"sa","collectCount":0,"commentCount":0,"image":"http://ssj.xxxxxxx.com.cn:11002/thumbnail.aspx?id=3E9B9D4813C74D0E82E2B0303119C3D2.png","sendDate":"2016-08-18 00:00:00","messageFrom":0},{"id":"7DE7CD6BE79A472B8D028147A212A127","title":"你想干啥  O(∩_∩)O","summary":"","author":"sa","collectCount":0,"commentCount":0,"image":"http://ssj.xxxxxxx.com.cn:11002/thumbnail.aspx?id=DBE075D396234C32820AE9794B668FC1.png","sendDate":"2016-08-19 00:00:00","messageFrom":0}],"isSubscribe":false},{"subscribe":{"id":"E145F4A60932490EA4DE06884CB05FBD","icon":"http://ssj.xxxxxxx.com.cn:11002/thumbnail.aspx?id=B9E47A6078D345298C60E47D5205BFBE.png","selectIcon":"http://ssj.xxxxxxx.com.cn:11002/thumbnail.aspx?id=B9E47A6078D345298C60E47D5205BFBE.png","label":"创新","logo":"http://ssj.xxxxxx.com.cn:11002/thumbnail.aspx?id=B9E47A6078D345298C60E47D5205BFBE.png","newsCount":1,"subscibeCount":0,"isSubscrible":false},"topNewsList":[{"id":"3614A3566E1A4B1A98592400D97CE1FE","title":"测试多一个","summary":"","author":"sa","collectCount":0,"commentCount":0,"image":"http://ssj.xxxxxxx.com.cn:11002/thumbnail.aspx?id=AC01389B6F284829827D92370341E549.png","sendDate":"2016-08-19 00:00:00","messageFrom":0}],"recommendNewsList":[],"moreNewsList":[],"isSubscribe":false}]}

-----------------------------------------------------------------------------------------

 

问题是怎么把json丢到页面去。而页面又怎么接。接了后又怎么传回后端。我对前端不熟,请大神不吝赐教。最好写的demo。

叶含风的主页 叶含风 | 初学一级 | 园豆:12
提问于:2016-08-20 14:41
< >
分享
最佳答案
0

$(function(){
    jsonPost();
});
function jsonPost(){    
    $("#btn").click(function(){
        var params01 = '[{"sex":"女","age":12,"address":"湖南"}{"sex":"男","age":22,"address":"海南"}]';
        $.post(WEB_ROOT+'/emp/jsonPost',{json: params},function(result){
            console.info("参数传递成功");
        });
    });
    
    /*
     * JSON 插件将js的json对象转化为字符后传入后台
     * $("#btn").click(function(){
        var jsonParams = [];
        var params01 = {
            sex : "女",
            age : 12,
            address : "湖南"
        };
        var params02 = {
            sex : "男",
            age : 32,
            address : "海南"
        }
        jsonParams.push(params01);
        jsonParams.push(params02);
        var params = JSON.stringify(jsonParams);
        $.post(WEB_ROOT+'/emp/jsonPost',{json: params},function(result){
            console.info("参数传递成功");
        });
    });*/
}

收获园豆:180
[3] | 小虾三级 |园豆:1174 | 2016-08-20 14:45

这个只是把页面数据丢到后端吧?!我还需要把后端数据丢到页面去。就是页面要怎么接过来。也是这样用吗?

叶含风 | 园豆:12 (初学一级) | 2016-08-20 15:16

@叶含风: @RequestMapping("/empIndx")
    public String empIndex(Model model){
        Integer index = iEmpService.getEmpIndex();
        model.addAttribute("index", index);
        return "/views/easyui/emp.jsp";
    }这个html页面就可以接收,或者js访问一般都有回调函数,用@responsebody,接收json

[3] | 园豆:1174 (小虾三级) | 2016-08-20 15:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册