2012-10-18 81 views
0

JSFiddle 我已经给出了一个示例,我试图获取JSON数据,但它不是预期的。如何发送表单数据(对象)数组作为json

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

我想提前

+0

我相信,服务器将无论如何组合重复键,是否有一个具体的问题,你试图解决这种格式? – Igor

+0

我认为你需要在预期的输出中切换你的'[]'和'{}'。 –

+0

鉴于JSON只是一个大概的想法,请检查链接 – Tribhuwan

回答

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; 
}; 
+0

我的JS foo是可怜的,但这应该是一般的想法。有人请根据需要清理。 –

+0

样本json是由我自己写的,它并不完美。我想要的是对象数组(其中对象包含名字,姓氏和性别),以便我可以直接将其转换为java对象 – Tribhuwan

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; 
这个数据作为 {[{"txtTitle":"Tribhuwan","txtName":"Dewangan","seGender":"Male"}, {"txtTitle":"Pankaj","txtName":"Sharma","seGender":"Male"}]} 感谢

我只是希望这个服务你,那种没有青睐于我,我是通过httppost客户端的响应有我sontent,存储在构建器变量中。

相关问题