如何在json数组下面发布内容?
"LstUserOptions": [
{
"OptionID": "ca339e40-10cc-4459-b9ec-07f7df0f4c69"
}
]
答案 0 :(得分:1)
我找到了解决方法:
List<Map> carOptionJson = new List();
CarJson carJson = new CarJson("ca339e40-10cc-4459-b9ec-07f7df0f4c69");
carOptionJson.add(carJson.TojsonData());
var body = json.encode({
"LstUserOptions": carOptionJson
});
http.Response response = await http.post(
Uri.encodeFull(ConfigApi.SAVE),
body: body,
headers: {'Content-type': 'application/json'});
class CarJson {
String OptionID;
CarJson(this.OptionID);
Map<String, dynamic> TojsonData() {
var map = new Map<String, dynamic>();
map["OptionID"] = OptionID;
return map;
}
}
答案 1 :(得分:0)
如何使用dio包完成这项工作我用dio发送了整个数据,现在我想用它发送一个json数组,如果假设我的json数组是body,则此代码是正确的
private void sendData(ArrayList list) throws JSONException {
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, YOUR_URL, setJsonObject(list)/*This is the function you need*/, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage(),Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(LogInActivity.this);
requestQueue.add(jsonObjReq);
}
//create json object that contain ArrayList contents
private JSONObject setJsonObject(َArrayList list) throws JSONException {
JSONObject jsonobject_one = new JSONObject();
JSONArray jsonArray = new JSONArray();
for(int i =0 ; i<list.size ; i++) {
JSONObject jsonobject = new JSONObject();
jsonobject .put("param"+i, list.get(i) );
jsonArray.put(jsonobject);
}
jsonobject_one.put("params_array",jsonArray);
return jsonobject_one;
}