这是我从服务器获取的数据格式。
[
{
"rid":"1",
"srid":"0",
"estimated_value":"100000",
"expected_close_date":"2014-11-22",
"sid":"2",
"others":null
},
{
"rid":"1",
"srid":"6",
"estimated_value":0,
"expected_close_date":null,
"sid":"2",
"others":null
},
{
"rid":"1",
"srid":"7",
"estimated_value":0,
"expected_close_date":null,
"sid":"2",
"others":null
},
{
"rid":"11",
"srid":"0",
"estimated_value":"300000",
"expected_close_date":"2014-11-14",
"sid":"1",
"others":null
},
{
"rid":"11",
"srid":"38",
"estimated_value":0,
"expected_close_date":null,
"sid":"1",
"others":null
},
{
"rid":"11",
"srid":"39",
"estimated_value":0,
"expected_close_date":null,
"sid":"1",
"others":null
}
]
接收此JSON对象并设置为我的表单。我可以再做一些修改,我必须以相同的格式将它发送到服务器。如何在JSON中发送空值?如果我在不给出引号的情况下给出null值,则JSON对象中的键和值不可用。
如果我在双引号内给它,那么它就像"others" : "null"
。但是我发送数据,它不会去服务器。当我以其他形式这样做时,这不会发生。其他形式必须具有所有字段的值。但在这种形式,我也可以有空值。
我想知道如何实现这一目标?
请帮帮我。
这是我的java代码。
RequestParams params = new RequestParams();
JSONArray req_array = new JSONArray();
Integer estim = estimatedModified.size();
for(int g=0;g<selectedReq.size();g++)
{
JSONObject req_object = new JSONObject();
try {
req_object.put("rid", selectedReq.get(g).toString());
req_object.put("srid", 0);
req_object.put("sid", 1);
req_object.put("estimated_value", estimatedModified.get(g));
req_object.put("expected_close_date", expectedCloseModified.get(g));
req_object.put("others", "");
req_array.put(req_object);
} catch (JSONException e) {
e.printStackTrace();
}
}
for (int j = 0; j < selectedSubReq.size(); j++) {
selectedSubReq.get(j);
JSONObject req_object = new JSONObject();
try {
SubRequirement temp = SubRequirement.getSubRequirement(selectedSubReq.get(j));
req_object.put("rid", temp.requirement_id);
req_object.put("srid", selectedSubReq.get(j).toString());
req_object.put("sid", 2);
req_object.put("estimated_value", 0);
req_object.put("expected_close_date", "null");
req_object.put("others", "null");
req_array.put(req_object);
} catch (JSONException e) {
e.printStackTrace();
}
}
params.add("req", req_array.toString());
System.out.println("ReqArray" + req_array.toString());
AsyncClientHandler.post("projectRequirements/save/"+proj.p_id, params,
new AsyncHttpResponseHandler() {
@Override
public void onFailure(int statusCode,
Throwable error, String content) {
super.onFailure(statusCode, error, content);
Toast.makeText(getActivity(), "failiure push"+proj.p_id,Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode,
Header[] headers, String content) {
super.onSuccess(statusCode, headers, content);
Toast.makeText(getActivity(), "success push",Toast.LENGTH_SHORT).show();
}
});
}
答案 0 :(得分:0)
在json中绑定值之前,只需检查它是否为NULL然后绑定&#34;&#34; (空白字符串)else绑定原始字符串..
答案 1 :(得分:0)
试试这个
if(json.isNull("others")) {
othersStr = null;
} else {
othersStr = json.getString("others");
}
HttpClient client=new DefaultHttpClient();
HttpPost req=null;
List<NameValuePair> reqParams = new ArrayList<NameValuePair>();
reqParams.add(new BasicNameValuePair("RequestJson", reqObj.toString()));
req=new HttpPost(appUrl);
req.setEntity(new UrlEncodedFormEntity(reqParams));
HttpResponse res=client.execute(req);