【问题标题】:Retriev the value of array of objects in servlet检索servlet中对象数组的值
【发布时间】:2014-09-06 11:41:34
【问题描述】:

如何在 servlet 中检索对象数组。

我在 javascript 中有一个表单的数组对象

var studentArr ={
                 {id: 1 , name : pUJA , mob : 455},
                 {id:2, name : Pinky , mob : 598}
                };

从 javascript 传递时如何在 servlet 中检索数组

【问题讨论】:

  • 是数组还是json数组?
  • 我在javascript中形成的数组对象
  • 使用 inputtype hidden 并与下一个请求一起发送。
  • 请指导我如何在 servlet 中检索数组

标签: java servlets


【解决方案1】:

你的例子:

var studentArr = {data: [{id: 1 , name : pUJA , mob : 455},{id:2, name : Pinky , mob : 598}]}; // place [ and ] and make it in json format then

你将不得不做一些这样的效果:

JSONObject myjson = new JSONObject(studentArr );
JSONArray the_json_array = myjson.getJSONArray("data");

这会返回数组对象。

那么迭代会如下:

    int size = the_json_array.length();
    ArrayList<JSONObject> arrays = new ArrayList<JSONObject>();
    for (int i = 0; i < size; i++) {
        JSONObject another_json_object = the_json_array.getJSONObject(i);
            //Blah blah blah...
            arrays.add(another_json_object);
    }

//Finally
JSONObject[] jsons = new JSONObject[arrays.size()];
arrays.toArray(jsons);

//The end...

您必须确定数据是否为数组(只需检查 charAt(0) 是否以 [ 字符开头)。

希望这会有所帮助。

Anthoer Java json tute 在这里....

【讨论】:

  • 我想使用 servlet 而不是 json。
  • 不是一个有效的 JSON ....虽然 ... { "data": [ { "id": 1, "name": "pUJA", "mob": 455 }, { " id": 2, "name": "Pinky", "mob": 598 } ] }`
  • 我需要从我传递给 servlet 的 javascript 中检索数组。如何在 servlet 中检索它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
  • 2015-01-31
  • 2021-06-23
  • 2020-08-22
  • 2013-06-22
相关资源
最近更新 更多