我有一个必须返回 JSON 的 wcf 服务。我的代码如下所示:
public String areaGetStreetTypes(int city_id, int language_id)
{
string json = "";
string responseList = "{\"triname\":\"IMP\",\"name\":\"IMPASSE\"}";
if (responseList.Length != 0){
string responseStatusJSON = "{\"status\":0, \"message\":\"Success !!!\"}";
json += "{\"responseList\":[" + responseList + "],";
json += "\"responseStatus\":" + responseStatusJSON + "}";
}
else{
string responseStatusJSON = "{\"status\":1, \"message\":\"Error !!!\"}";
json += responseStatusJSON;
}
return json;
}
//my interface looks like
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "areaGetStreetTypes", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
String areaGetStreetTypes(int city_id, int language_id);
原始格式的响应:
{"areaGetStreetTypesResult":"{\"responseList\":[{\"triname\":\"IMP\",\"name\":\"IMPASSE\"}],\"responseStatus\":{\"状态\":0, \"消息\":\"成功!!!\"}}"}
我使用 php 脚本进行测试。第一次解码后:
标准类对象(
[areaGetStreetTypesResult] => {"responseList":[{"triname":"IMP","name":"IMPASSE"}],"responseStatus":{"status":0, "message":"Success !!!"}}
)
只有在我的第二个 json_decode 之后,我才得到我想要的:GOOD JSON Response
我可以在我的服务中进行哪些更改以第一次获得良好的 JSON?我只想解码一次。