Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从数据库中获取数据、hashmap、将hashmap转换为JSONObject列表并在javascript中使用_Javascript_Json_Postgresql_List_Hashmap - Fatal编程技术网

如何从数据库中获取数据、hashmap、将hashmap转换为JSONObject列表并在javascript中使用

如何从数据库中获取数据、hashmap、将hashmap转换为JSONObject列表并在javascript中使用,javascript,json,postgresql,list,hashmap,Javascript,Json,Postgresql,List,Hashmap,我正在使用(tomcat、jsp、postgres、googlemap等)开发一个java web项目。postgres数据库表有7列,其中一列为地理数据类型。因此,我使用Hashmap检索数据,并使用以下方法将其放入ArrayList中,该方法接收ResultSet对象 public ArrayList resultSetToArrayList(ResultSet rs) throws SQLException{ ResultSetMetaData md = rs.getMetaDa

我正在使用(tomcat、jsp、postgres、googlemap等)开发一个java web项目。postgres数据库表有7列,其中一列为地理数据类型。因此,我使用Hashmap检索数据,并使用以下方法将其放入ArrayList中,该方法接收ResultSet对象

  public ArrayList resultSetToArrayList(ResultSet rs) throws SQLException{

  ResultSetMetaData md = rs.getMetaData();
  int columns = md.getColumnCount();
  ArrayList list = new ArrayList();
  ArrayList<HashMap<String, Object>> dataMap = new 
  ArrayList<HashMap<String,Object>>();

  while (rs.next()){
  HashMap row = new HashMap(columns);
        for(int i=1; i<=columns; ++i){                          
            row.put(md.getColumnName(i),rs.getObject(i));
        }
        list.add(row);
    }
  return list;
}
公共ArrayList resultSetToArrayList(ResultSet rs)引发SQLException{ ResultSetMetaData md=rs.getMetaData(); int columns=md.getColumnCount(); ArrayList=新建ArrayList(); ArrayList数据映射=新建 ArrayList(); while(rs.next()){ HashMap行=新的HashMap(列);
对于(inti=1;i*******我修复了这个问题*********
我只需要通过执行json=json.parse(json)将json变量转换回json对象;以确保js不会将其视为字符串。这为我解决了问题,现在我可以通过obj.image循环获取图像。感谢Ehren Murdick花时间阅读和回复。

使用JSON.parse有什么不好的地方?这是有效的JSON,应该可以解析。(我使用我如何使用JSON.parse函数获取图像路径并将其保存在一个名为img的var中?var JSON=''for(var I=0;IArrayList<HashMap<String, Object>> hashmapData = newArrayList<HashMap<String, Object>>(); hashmapData = api.resultSetToArrayList(rs);//api is an instance of my APIService class that contains the method List<JSONObject> jsonObj = new ArrayList<JSONObject>(); for(HashMap<String, Object> data : hashmapData) { JSONObject obj = new JSONObject(data); jsonObj.add(obj); } request.setAttribute("data",jsonObj); //Disptching request RequestDispatcher dispatcher = request.getRequestDispatcher("displayallobjects.jsp"); if (dispatcher != null){ dispatcher.forward(request, response); } ..... ....//rest of the code }
  <script>
        var jsondata = '<%=request.getAttribute("data") %>'
  ....
  </script>

 [{"id":49,"location":"POINT(43.5729914065888 -79.7675657272339)", 
 "questid":3,"image":"images\/science.jpg","type":"science"}, 
 {"id":51,"location":"POINT(43.5725872088145 -79.7640466690063)", 
 "questid":5,"image":"images\/math.jpg","type":"math"}, 
 {"id":52,"location":"POINT(43.5730224986129 -79.7701406478882)", 
 "questid":2,"image":"images\/math.jpg","type":"math"}, 
 {"id":53,"location":"POINT(43.5742972577808 -79.7683811187744)", 
 "questid":1,"image":"images\/english.jpg","type":"english"}, 
 {"id":54,"location":"POINT(43.5707838318471 -79.7665357589722)", 
 "questid":9,"image":"images\/science.jpg","type":"science"}]