【问题标题】:Passing Arrays in array JSON from Android to rails server将数组 JSON 中的数组从 Android 传递到 Rails 服务器
【发布时间】:2014-06-18 08:52:18
【问题描述】:

这应该是 JSON 输出:

{
"ph_immunizations_attributes": [
       {"immunization_id": 1},
       {"immunization_id": 2}
]
}

我将如何在我的 Android 代码中翻译它以发布到我的 Rails 服务器中

这是代码:

List<NameValuePair> immunizatioValuePairs = new ArrayList();
JSONObject jObj = new JSONObject();
JSONObject main = new JSONObject();
JSONArray jA = new JSONArray();
jObj.put("immmunization_id", 1);

jA.put(jObj);
main.put("ph_immunizations_attributes", jA);


immunizatioValuePairs.add(new BasicNameValuePair("ph_immunizations_attributes",             main.toString()));
httppost.setEntity(new UrlEncodedFormEntity(immunizatioValuePairs));

【问题讨论】:

    标签: android ruby-on-rails arrays json object


    【解决方案1】:
    JSONObject jObj = new JSONObject();
    JSONObject main = new JSONObject();
    JSONArray jA = new JSONArray();
    jObj.put("immunization_id", 1);  // 1 --> get the values here either from other class or array or whatever.
    
    jA.put(jObj);
    main.put("ph_immunizations_attributes", jA);
    

    试试这个,让我知道

    【讨论】:

    • 但是当我转换 JSON 并将其传递给服务器时,如何删除反斜杠?
    • 这里的反斜杠在哪里?
    • 当我们检查服务器日志时,这是带有反斜杠的结果:{"ph_immunizations_attributes"=>"{\"ph_immunizations_attributes\":[{\"immunization_id\":1}]}" }
    • 你如何处理 Rails 应用程序中的关联? user_id 插入有问题@Aniruddha
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 2017-05-06
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    相关资源
    最近更新 更多