【发布时间】:2016-12-08 07:35:02
【问题描述】:
我想使用 Alamofire(V3.5.1),我正在使用 Swift(V2.3)。我要发布的 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.
Q1:如何在正文中发布正确的嵌套 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}"
}
}
]
}
编辑:这是关于如何在 JAVA 中使用短语响应的 API 文档
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是一个字符串。
Q2:如何分析这种JSON?我只想要dataValue,谢谢
【问题讨论】:
-
您可以发布您的 Alamofire 代码吗? 注意:必要时可以隐藏网址
-
你得到的参数是正确的imo,在你的
api中你确定服务器接受application/json类型的参数吗?我不明白你在“我希望得到回应”之后的意思,你能改述一下吗? -
@ZonilyJame 我的意思是服务器会给我这个响应,这是 API 文档中的一个正确示例。我使用这个 Alamofire 方法`Alamofire.request(.POST, url, parameters: parameters,编码:.JSON,标头:标头).responseJSON `
-
我没有更多的swift代码,只需将'dataValue'更改为字符串
-
非常感谢