0

JSFiddle 中,我给出了一个示例,我正在尝试获取 JSON 数据,但并不如预期。

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

我想要这个数据{[{"txtTitle":"Tribhuwan","txtName":"Dewangan","seGender":"Male"}, {"txtTitle":"Pankaj","txtName":"Sharma","seGender":"Male"}]} 提前谢谢

4

2 回答 2

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 客户端响应获得了我的 Sontent,存储在 builder 变量中。

于 2012-10-18T13:58:59.740 回答
0

您想要的输出无效

如果你能忍受一个输出

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

然后链接示例中的这个 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;
};
于 2012-10-18T14:12:51.717 回答