Extract JSON Value



我在尝试从JSON中提取值时遇到了一些问题。以下是从我的servlet返回的JSON:

[
    {
        "eventDesc":"My sons 5th year birthday party",
        "eventDate":"12/11/2014",
        "eventID":"1",
        "eventName":"Birthday party",
        "eventTime":"17:00",
        "eventX":"41803.2",
        "eventY":"38210.8",
        "eventBy":"Gabriel"
    },
    {
        "eventDesc":"Steamboat Gathering",
        "eventDate":"20/11/2014",
        "eventID":"2",
        "eventName":"Gathering",
        "eventTime":"19:00",
        "eventX":"41551.6",
        "eventY":"38211.7",
        "eventBy":"JunHong"
    }
]

我不知道该如何提取每条记录,因为我必须将每条记录绘制在地图上。

我有以下代码要从servlet中返回的JSON中提取:

public void getThemesOnMap() throws JSONException{
    String page;
    BufferedReader in;
    JSONArray jsonArray;
    try{
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://localhost:8080/MyProject/MyServlet");
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while((line = in.readLine()) != null){
            sb.append(line + NL);
        }
        in.close();
        page = sb.toString();
        System.out.println(page);
        try{
            JSONObject jsonObject = new JSONObject(page);
            jsonArray = jsonObject.getJSONArray(page);
            int length = jsonArray.length();
            for(int i = 1; i < length; i++){
                JSONObject attribute = jsonArray.getJSONObject(i);
                String eventName = attribute.getString("eventName");
                String eventX = attribute.getString("eventX");
                String eventY = attribute.getString("eventY");
                PictureMarkerSymbol graphicIcon;
                graphicIcon = new PictureMarkerSymbol(this.getResources().getDrawable(R.drawable.busstopicon));
                Point p = new Point(Double.parseDouble(eventX), Double.parseDouble(eventY));
                Symbol symbol = graphicIcon;
                HashMap<String, Object> attrMap = new HashMap<String, Object>();
                attrMap.put("eventName", eventName);
                graphicsLayer.addGraphic(new Graphic(p, symbol, attrMap));
            }
        }
        catch(JSONException e){
            e.printStackTrace();
        }
    }
    catch(IOException e){
        e.printStackTrace();
    }
}

但它并没有在地图上画出任何东西。有什么想法吗?

提前谢谢。

编辑

我如何格式化JSON以返回为:

{Events:[
{
    "eventDesc":"My sons 5th year birthday party",
    "eventDate":"12/11/2014",
    "eventID":"1",
    "eventName":"Birthday party",
    "eventTime":"17:00",
    "eventX":"41803.2",
    "eventY":"38210.8",
    "eventBy":"Gabriel"
},
{
    "eventDesc":"Steamboat Gathering",
    "eventDate":"20/11/2014",
    "eventID":"2",
    "eventName":"Gathering",
    "eventTime":"19:00",
    "eventX":"41551.6",
    "eventY":"38211.7",
    "eventBy":"JunHong"
}]
}

这是我从数据库检索数据的servlet方法:

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    if(request.getParameter("SQL1")!=null){
    }
    JSONArray jsonArray = new JSONArray();
    PrintWriter out = response.getWriter();
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost/mydb", "root", "root");
        PreparedStatement statement = con
                .prepareStatement("SELECT * FROM event");
        ResultSet result = statement.executeQuery();
        while (result.next()) {
            JSONObject eventInfo = new JSONObject();
            eventInfo.put("eventID", result.getString("eventID"));
            eventInfo.put("eventName", result.getString("eventName"));
            eventInfo.put("eventDesc", result.getString("eventDesc"));
            eventInfo.put("eventDate", result.getString("eventDate"));
            eventInfo.put("eventTime", result.getString("eventTime"));
            eventInfo.put("eventX", result.getString("eventX"));
            eventInfo.put("eventY", result.getString("eventY"));
            eventInfo.put("eventBy", result.getString("eventBy"));
            jsonArray.put(eventInfo);
        }
    }
    catch (JSONException je) {
        System.out.println(je.getMessage());
    } catch (Exception exc) {
        System.out.println(exc.getMessage());
    }
    out.println(jsonArray.toString());
}

创建JSONArray

JSONArray arr = new JSONArray("your json string here");

并浏览

for (int i = 0; i < arr.length; i++)
{
    JSONObject o = arr.getJSONObject(i);
     //...

更多信息请点击此处:http://www.json.org/javadoc/index.html?org/json/JSONObject.html

编辑

这样做:

jsonArray = new JSONArray(page);
int length = jsonArray.length();
for(int i = 0; i < length; i++){
    //...

编辑2

要获得所需的JSON,请执行以下操作:

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    if(request.getParameter("SQL1")!=null){
    }
    JSONObject result = new JSONObject();
    JSONArray jsonArray = new JSONArray();
    PrintWriter out = response.getWriter();
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost/mydb", "root", "root");
        PreparedStatement statement = con
                .prepareStatement("SELECT * FROM event");
        ResultSet result = statement.executeQuery();
        while (result.next()) {
            JSONObject eventInfo = new JSONObject();
            eventInfo.put("eventID", result.getString("eventID"));
            eventInfo.put("eventName", result.getString("eventName"));
            eventInfo.put("eventDesc", result.getString("eventDesc"));
            eventInfo.put("eventDate", result.getString("eventDate"));
            eventInfo.put("eventTime", result.getString("eventTime"));
            eventInfo.put("eventX", result.getString("eventX"));
            eventInfo.put("eventY", result.getString("eventY"));
            eventInfo.put("eventBy", result.getString("eventBy"));
            jsonArray.put(eventInfo);
        }
        result.put("Events", (Object) jsonArray);
    }
    catch (JSONException je) {
        System.out.println(je.getMessage());
    } catch (Exception exc) {
        System.out.println(exc.getMessage());
    }
    out.println(result.toString());
}

如果您想映射json,您可能需要使用现有的javascript/jquery库但为了让你知道,我假设你想创建一个时间线图,所以这里的算法是:

you need to loop over dates check any event from this date from you json by looping through it if there are events then put it to the map horizontally then start the next loop to check.

希望它能帮助

您是否考虑过使用spring框架的RestTemplate类?它直接将http请求与在大约5行代码中将JSON回复映射到pojo的可能性相结合。其优点是您知道映射工作正常,并且源代码更易于阅读。

对我帮助很大的例子:https://spring.io/guides/gs/consuming-rest/

相关内容

  • 没有找到相关文章

最新更新