【问题标题】:How do i parse a string with a JSON array to variables so i can put them in html tables?如何将带有 JSON 数组的字符串解析为变量,以便将它们放入 html 表中?
【发布时间】:2021-04-21 15:41:47
【问题描述】:

我有一个名为 Items 的 WebServlet,在 doGet 方法中我发送一个 JSON 数组,我接收该数组并将其放入一个字符串中,但现在我无法将其放回 JSON 对象中,因此我可以检索要放入的值在 html 页面中。

 HttpURLConnection con = (HttpURLConnection) new URL("http://localhost:8080/items/?get=item").openConnection();
 con.setRequestMethod("GET");
 con.setDoInput(true);
 con.connect();

 BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
 String stringJson = reader.readLine();
 JsonArray array = Json.createParser(stringJson);

我现在想要的是将对象放入数组中,以便获取值。 我已经尝试过使用 JsonParser,但我无法弄清楚。

【问题讨论】:

    标签: java html json rest servlets


    【解决方案1】:

    据我了解,您需要将 stringJson 传递给 Json。尝试使用org.json 库:

    try {
         JSONObject jsonObject = new JSONObject(stringJson);
    }catch (JSONException err){
         logger.error("Error", err.toString());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      相关资源
      最近更新 更多