qq_43198047
2018-09-13 08:45
采纳率: 50%
浏览 1.8k
已采纳

调用接口查到的数据如何遍历

{"resultcode":"200","reason":"查询成功!","result":[{"city":"北京","b90":"6.75","b93":"7.54","b97":"8.03","b0":"7.23"},{"city":"天津","b90":"6.67","b93":"7.54","b97":"7.96","b0":"7.18"},{"city":"河北","b90":"5.61","b93":"7.54","b97":"7.96","b0":"7.18"},{"city":"山西","b90":"5.64","b93":"7.49","b97":"8.09","b0":"7.25"},{"city":"内蒙古","b90":"","b93":"7.50","b97":"8.05","b0":"6.99"},{"city":"辽宁","b90":"5.86","b93":"7.51","b97":"8.10","b0":"7.10"},{"city":"吉林","b90":"6.10","b93":"7.51","b97":"8.10","b0":"7.10"},{"city":"黑龙江","b90":"","b93":"7.40","b97":"7.90","b0":"6.95"},{"city":"上海","b90":"6.62","b93":"7.51","b97":"7.99","b0":"7.16"},{"city":"江苏","b90":"6.66","b93":"7.51","b97":"7.99","b0":"7.14"},{"city":"浙江","b90":"5.99","b93":"7.51","b97":"7.99","b0":"7.16"},{"city":"安徽","b90":"6.37","b93":"7.50","b97":"8.03","b0":"7.22"},{"city":"福建","b90":"5.62","b93":"7.51","b97":"8.02","b0":"7.17"},{"city":"江西","b90":"6.60","b93":"7.50","b97":"8.06","b0":"7.23"},{"city":"山东","b90":"5.54","b93":"7.52","b97":"8.07","b0":"7.18"},{"city":"河南","b90":"5.68","b93":"7.55","b97":"8.06","b0":"7.17"},{"city":"湖北","b90":"5.40","b93":"7.55","b97":"8.09","b0":"7.17"},{"city":"湖南","b90":"6.64","b93":"7.49","b97":"7.97","b0":"7.25"},{"city":"广东","b90":"","b93":"7.56","b97":"8.19","b0":"7.19"},{"city":"广西","b90":"5.45","b93":"7.60","b97":"8.21","b0":"7.24"},{"city":"海南","b90":"7.61","b93":"8.66","b97":"9.19","b0":"7.27"},{"city":"重庆","b90":"6.53","b93":"7.61","b97":"8.04","b0":"7.25"},{"city":"四川","b90":"6.12","b93":"7.57","b97":"8.15","b0":"7.27"},{"city":"贵州","b90":"6.94","b93":"7.67","b97":"8.11","b0":"7.29"},{"city":"云南","b90":"6.70","b93":"7.69","b97":"8.25","b0":"7.25"},{"city":"西藏","b90":"7.23","b93":"8.42","b97":"8.91","b0":"7.72"},{"city":"陕西","b90":"5.67","b93":"7.43","b97":"7.85","b0":"7.08"},{"city":"甘肃","b90":"5.51","b93":"7.43","b97":"7.94","b0":"7.08"},{"city":"青海","b90":"5.85","b93":"7.50","b97":"8.04","b0":"7.11"},{"city":"宁夏","b90":"5.56","b93":"7.45","b97":"7.87","b0":"7.07"},{"city":"新疆","b90":"6.06","b93":"7.33","b97":"7.88","b0":"6.99"}],"error_code":0}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • 已采纳

    你一开始就应该把问题说清楚,获取了json数据,需要展示easyui的datagrid里。
    如果时展示在datagrid里,都不需要循环,直接返回这个json就行了
    官网有例子,就连json数据都给你打包好了,如果格式不符合返回的数据,你后台就要先解析处理数据,然后再打包成json数据。这里我就不写代码了。

    点赞 评论
  • hibey 2018-09-13 08:52

    var obj = data.parseJSON(); //由JSON字符串转换为对象

    点赞 评论
  • QQ口香糖 2018-09-13 08:59

    在js中用for循环遍历,先获取到result中的值然后这样
    for ( var i = 0; i < result.length; i++){
    console.log( result[i] );
    }
    console.log(result[i]); 打印出来的数据是你对应的单个对象

    点赞 评论
  • 是easyui下拉框填充省份嘛?身份数据后台获取。
    例子:

     页面代码:<td><input name="religionType" id="religionType" /> 
     <script type="text/javascript">
    $(function() {
    
    //宗教信仰
    $("#religionType").combobox({
                width: 135,
                editable: false,
                url: "caSysDictController.do?getDictList&dictCode=ZJXY",
                valueField: 'id',    //id
                textField: 'dictName' //对应名称
            });
    
    });
    
    </script>
    
    

    后台方法:

     @RequestMapping(params = "getDictList")    
    @ResponseBody
    public List<CaSysDict> getDictList(HttpServletRequest request) {
        String dictCode = request.getParameter("dictCode");
        return caSysDictService.getDictListByKey(dictCode); //查询数据
    }
    
    

    具体看我博客:https://blog.csdn.net/qq_23126581/article/details/82345652

    点赞 评论
  • 是后台遍历还是前台遍历?

     1、JsonObject
    
    使用JsonObject解析只有一条数据的json是非常方便的例如:"{\"name\":\"zhangsan\",\"password\":\"zhangsan123\",\"email\":\"10371443@qq.com\"}"
    
    
    public static void main(String[] args) {  
    
             String jsonString ="{\"name\":\"zhangsan\",\"password\":\"zhangsan123\",\"email\":\"10371443@qq.com\"}";  
             JSONObject json = JSONObject.fromObject(jsonString);  
             User user = new User();  
             user.setName(json.getString("name"));  
             user.setPassword(json.getString("password"));  
             user.setEmail(json.getString("email"));  
             System.out.println(user.toString());  
        }  
    
    
    2、JsonArray
    
    使用JsonArray解析数组数据的json是非常方便的例如:"[{\"name\":\"zhangsan\",\"password\":\"zhangsan123\",\"email\":\"10371443@qq.com\"},{\"name\":\"lisi\",\"password\":\"lisi123\",\"email\":\"1435123@qq.com\"}]"
    
    
    [java] view plain copy
    String json = <span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">"[{\"name\":\"zhangsan\",\"password\":\"zhangsan123\",\"email\":\"10371443@qq.com\"},</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">{\"name\":\"lisi\",\"password\":\"lisi123\",\"email\":\"1435123@qq.com\"}</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">]"</span>;  
             JSONArray jsonArray = JSONArray.fromObject(json);  
             ArrayList<User> users = new ArrayList<User>();  
             for (int i = 0; i < jsonArray.size(); i++) {  
                 User userM = new User();  
                 user.setName(jsonArray.getJSONObject(i).getString("name"));  
                 user.setpassword(jsonArray.getJSONObject(i).getString("password"));  
                 user.setEmail(jsonArray.getJSONObject(i).getString("email"));  
                 users.add(user);  
            }  
             for (User user : users) {  
                System.out.println(user.toString());  
            }  
    二、Jsp解析json数据
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>JqueryDemo1</title>
    <script src="jquery.js"></script>
    <script>
    
    function showData() {
        var str='{ "name": "John" }';//json标准格式
        var obj = eval('(' + str + ')'); 
        alert( obj.name);
    
    
        var str2="{ 'name': 'John' }";
        var obj2 = eval('(' + str2 + ')'); 
        alert( obj2.name);
    
    
        var str3='{"GetUserPostByIdResult":{"Age":"33","ID":"2server","Name":"mady"}}';
        var obj3 = eval('(' + str3 + ')'); 
        alert( obj3.GetUserPostByIdResult.ID);
    
        var data=" { root: [ {'name':'6200','value':'0'}, {'name':'6101','value':'xa'}, {'name':'6102','value':'beijing'}, {'name':'6103','value':'haerbin'}]}";
        var date1=eval('('+data+')');
        alert(date1.root[2].name);
    
    
      var last=JSON.stringify(obj);//将JSON对象转化为JSON字符
        alert(last);
    }
    
    
    </script>
    </head>
    <body>
    <input id="Button1" type="button" value="Hit Me" onclick="showData()" /> 
    
    
    </body>
    </html>
    
    点赞 评论
  • 前台解析差不多就是这样,你试试
    先解析json
    比如你用var str=你返回的数据
    然后解析:var obj = eval('(' + str + ')');
    for(int i=0;i<obj.result.length;i++)
    {
    alert(result[i].city);
    }
    差不多就是这样,你试试

    点赞 评论
  • sweet_yaya457 2018-09-13 09:46

    首先,你的数据是在返回的result中的,所以你先把这个字段取出来,来循环这个数组,假设将结果赋值为data,便利如下。
    data.map((item, index) => {
    //再次拼接字符串
    }

    点赞 评论

相关推荐 更多相似问题