将数组中的 Json 对象发送到服务器

Posted

技术标签:

【中文标题】将数组中的 Json 对象发送到服务器【英文标题】:Sending Json objects within array to server 【发布时间】:2016-12-15 04:23:10 【问题描述】:

我对此很陌生,我正在尝试通过 volley 在数组中发送动态对象。您可以在 http://www.jsoneditoronline.org/ 上查看此内容。 我在编码上摸索了,所以任何帮助都将不胜感激。 下面是我的代码的 sn-p,其中对象是动态的,我将从微调器中选择并将它们发送到服务器

[
   
      "longDescription":"Carrot Cheese Paratha is perfect to attract kids as it is colorful and yummy...",
      "productName":"CARROT CHEESE PARATHA",
      "name":"Delicious Kids Meal",
      "productId":"Monday",
      "catalogName":"KIDS TIFFIN MENU",
      "categoryName":"Monday",
      "subCategoryName":"Long Break",
      "kidId":47
   ,
   
      "longDescription":"Freshly cooked Desi Ghee Paratha along with Paneer butter masala",
      "productName":"Paratha plus paneer butter masala",
      "name":"Delicious Kids Meal",
      "productId":"Monday",
      "catalogName":"KIDS TIFFIN MENU",
      "categoryName":"Monday",
      "subCategoryName":"Short Break",
      "kidId":47
   ,
   
      "longDescription":"Basmati Rice along with freshly cooked Matar Paneer (cottage Cheese)",
      "productName":"Matar paneer and Basmati Rice",
      "name":"Delicious Kids Meal",
      "productId":"Wednesday",
      "catalogName":"KIDS TIFFIN MENU",
      "categoryName":"Wednesday",
      "subCategoryName":"Short Break",
      "kidId":47
   
]

【问题讨论】:

【参考方案1】:

你可以使用 JsonArrayRequest 来做到这一点

RequestQueue requestQueue = Volley.newRequestQueue(this);
    final String jsonString = "your json here";
    try 
        JSONArray jsonArray = new JSONArray(jsonString);
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, url, jsonArray, new Response.Listener<JSONArray>() 
            @Override
            public void onResponse(JSONArray response) 
                //
            
        , new Response.ErrorListener() 
            @Override
            public void onErrorResponse(VolleyError error) 
                //
            
        );
        requestQueue.add(jsonArrayRequest);
     catch (JSONException e) 
        e.printStackTrace();
    

【讨论】:

【参考方案2】:

你的模型类。

public class test 

/**
 * longDescription : Carrot Cheese Paratha is perfect to attract kids as it is colorful and yummy...
 * productName : CARROT CHEESE PARATHA
 * name : Delicious Kids Meal
 * productId : Monday
 * catalogName : KIDS TIFFIN MENU
 * categoryName : Monday
 * subCategoryName : Long Break
 * kidId : 47
 */

private String longDescription;
private String productName;
private String name;
private String productId;
private String catalogName;
private String categoryName;
private String subCategoryName;
private int kidId;

public String getLongDescription() 
    return longDescription;


public void setLongDescription(String longDescription) 
    this.longDescription = longDescription;


public String getProductName() 
    return productName;


public void setProductName(String productName) 
    this.productName = productName;


public String getName() 
    return name;


public void setName(String name) 
    this.name = name;


public String getProductId() 
    return productId;


public void setProductId(String productId) 
    this.productId = productId;


public String getCatalogName() 
    return catalogName;


public void setCatalogName(String catalogName) 
    this.catalogName = catalogName;


public String getCategoryName() 
    return categoryName;


public void setCategoryName(String categoryName) 
    this.categoryName = categoryName;


public String getSubCategoryName() 
    return subCategoryName;


public void setSubCategoryName(String subCategoryName) 
    this.subCategoryName = subCategoryName;


public int getKidId() 
    return kidId;


public void setKidId(int kidId) 
    this.kidId = kidId;

我编写的 VolleyConnection 类中的 VolleyJsonPost 方法:

public static void jsonPost(Context context, String url, JSONObject jsonObject, Response.Listener<JSONObject> responceListener, Response.ErrorListener errorListener)

    JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST,url,jsonObject,responceListener,errorListener);

    Volley.newRequestQueue(context).add(jsonRequest);

而你使用这种方式:

VolleyConnection.jsonPost(context, rootUrl, jsonObject, new Response.Listener<JSONObject>() 
        @Override
        public void onResponse(JSONObject response) 


            try 
                // do stuff
             catch (JSONException e) 
                e.printStackTrace();
            
        
    , new Response.ErrorListener() 
        @Override
        public void onErrorResponse(VolleyError error) 
            error.printStackTrace();
        
    );


【讨论】:

以上是关于将数组中的 Json 对象发送到服务器的主要内容,如果未能解决你的问题,请参考以下文章

Ajax 将带有两个数组的 JSON 对象发送到一个 servlet 并在没有 jQuery 的情况下在 java servlet 中解析

如何将 JSON 对象从 Python 脚本发送到 jQuery?

AFNetworking 2.0 将 [] 方括号添加到发送到服务器的 json

如何通过 iOS 中的 JSON webservices post 方法将一组图像发送到服务器?

使用 jQuery 将 JSON 发送到服务器

ASIHTTPRequest 将 json 发送到 php 服务器