如何将表单数据(Object)的数组作为json发送

时间:2012-10-18 13:22:15

标签: jquery json

JSFiddle 我已经提供了一个示例,我正在尝试获取JSON数据,但它并不像预期的那样。

{"txtTitle":["Tribhuwan","Pankaj"],"txtName":["Dewangan","Sharma"]
,"seGender":["Male","Male"]}

我希望这些数据为{[{"txtTitle":"Tribhuwan","txtName":"Dewangan","seGender":"Male"}, {"txtTitle":"Pankaj","txtName":"Sharma","seGender":"Male"}]} 提前致谢

2 个答案:

答案 0 :(得分:0)

JSONObject myjson ;
JSONArray the_json_array;
StringBuilder builder = ... your jason content by buffer .....
            String a = "{child:"+builder.toString()+"}";
            myjson = new JSONObject(a);
            the_json_array = myjson.getJSONArray("child");
            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);
                    arrays.add(another_json_object);
            }
        } catch (ClientProtocolException e) {
            System.out.println("ClientProtocolException :"+e);
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IOException :"+e);
            e.printStackTrace();
        } catch (JSONException e) {
            System.out.println("JSONException :"+e);
            e.printStackTrace();
        }
        return arrays;

我希望这能为你服务,对我有所帮助,我通过httppost客户端响应获得了我的声音,存储在构建器变量中。

答案 1 :(得分:0)

您想要的输出是invalid

如果你可以使用

的输出
[{"txtTitle":"Tribhuwan","txtName":"Pankaj","seGender":"Male"},{"txtTitle":"Dewangan","txtName":"Sharma","seGender":"Male"}]

然后来自链接的example的这个serializeObject方法可以工作

$.fn.serializeObject = function() {
    var o = [];
    var a = this.serializeArray();
    var t = {};
    $.each(a, function() {

        if(t[this.name] !== undefined){
            o.push(t);
            t = {};
        }

        t[this.name] = this.value;        
    });
    o.push(t);
    return o;
};