我想使用Alamofire(V3.5.1),並且我正在使用Swift(V2.3)。我想發佈的JSON是這個。如何使用Alamofire發佈嵌套的JSON?
{
"inputs": [
{
"image": {
"dataType": 50,
"dataValue": "base64_image_string"
},
"configure": {
"dataType": 50,
"dataValue": "{\"side\":\"face\"}"
}
}
]
}
我儘量讓Alamofire
參數,這樣
let parameters : [String: AnyObject] = [
"inputs" : [
[ "image":[
"dataType":50,
"dataValue":(base64String)
],
"configure":[
"dataTpye":50,
"dataValue": ["side" :"face"]
]
]
]
]
但結果我得到的是這樣的。 FAILURE: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0.
問題1:我如何在正文中發佈正確的嵌套json?
編輯:我試圖用@Zonily Jame的方式來創建JSON對象,但它失敗了。這裏是我的代碼:
let imageData:[String:AnyObject] = ["dataType":50, "dataValue":"string"]
let configureData:[String:AnyObject] = ["dataType":50, "dataValue":"{\"side\":\"face\"}"]
let inputsData:[String:AnyObject] = ["image":dictToJSON(imageData) , "configure":dictToJSON(configureData)]
let parameters:[String:AnyObject] = ["inputs":dictToJSON(inputsData)]
,我打印的parameters
變量,它是這樣的:
["inputs": {
configure = {
dataType = 50;
dataValue = {
side = face;
};
};
image = {
dataType = 50;
dataValue = "";
};
}]
不知何故,語法仍然不正確。而且我還試圖在變量configureData
上使用dictToJSON()
,我仍然得到了相同的結果。
的預期反應應該是
{
"outputs": [
{
"outputLabel": "ocr_id",
"outputMulti": {},
"outputValue": {
"dataType": 50,
"dataValue": "{\"address\": \"string\", \"config_str\" : \"{\"side\":\"face\"}\", \"name\" : \"Jack\",\"num\" : \"1234567890\", \"success\" : true}"
}
}
]
}
編輯:這是API的有關如何短語迴應,但在JAVA
try {
JSONObject resultObj = new JSONObject(result);
JSONArray outputArray = resultObj.getJSONArray("outputs");
String output = outputArray.getJSONObject(0).getJSONObject("outputValue").getString("dataValue");
JSONObject out = new JSONObject(output);
if (out.getBoolean("success")) {
String addr = out.getString("address");
String name = out.getString("name");
String num = out.getString("num");
System.out.printf(" name : %s \n num : %s\n address : %s\n", name, num, addr);
} else {
System.out.println("predict error");
}
} catch (JSONException e) {
e.printStackTrace();
}
,並請求代碼文件
public static JSONObject getParam(int type, JSONObject dataValue) {
JSONObject obj = new JSONObject();
try {
obj.put("dataType", type);
obj.put("dataValue", dataValue);
} catch (JSONException e) {
e.printStackTrace();
}
return obj;
}
public static JSONObject getParam(int type, String dataValue) {
JSONObject obj = new JSONObject();
try {
obj.put("dataType", type);
obj.put("dataValue", dataValue);
} catch (JSONException e) {
e.printStackTrace();
}
return obj;
}
JSONObject requestObj = new JSONObject();
try {
JSONObject configObj = new JSONObject();
JSONObject obj = new JSONObject();
JSONArray inputArray = new JSONArray();
configObj.put("side", configStr);
obj.put("image", getParam(50, imgBase64));
obj.put("configure", getParam(50, configObj.toString()));
inputArray.put(obj);
requestObj.put("inputs", inputArray);
} catch (JSONException e) {
e.printStackTrace();
}
String body = requestObj.toString();
注意:imgBase64
是一個字符串。
問題2:我該如何分析這種JSON?我只想要dataValue
,謝謝
您可以發佈您Alamofire代碼? **注:**,你可以隱藏在必要時配偶 –
你得到的參數是正確的海事組織,在'api'你確定服務器是由接受申請'/ json'類型參數的網址是什麼?在「我準備得到迴應」之後,我不明白你的意思嗎? –
@ZonilyJame我的意思是服務器會給我這個迴應,這是API的Document.And我使用這個Alamofire方法'Alamofire.request(.POST, url, 參數,參數, 編碼:.JSON , 標題:標題).responseJSON' – Shucheng