我试图在顶线上模拟import AppDispatcher from '../../Dispatcher/AppDispatcher';
import ItemApi from '../../Utils/API/Items/AuthApi';
import ItemConstants from '../../Constants/Items/ItemConstants';
var ItemAction = {
getItems: function() {
ItemApi.getItems().then(
function(response) {
AppDispatcher.handleServerAction({
type: ItemConstants.GET_ITEMS_SUCCESS,
items: response.data
});
}
)
.catch(
function(error) {
AppDispatcher.handleServerAction({
type: ItemConstants.GET_ITEMS_FAILURE
});
}
);
}
};
export default ItemAction;
以进行单元测试,以返回一个任务,其中字符串是下面的JSON:
ReadAsStringAsync
如何将此有效JSON转换为var jsonString = await response.Content.ReadAsStringAsync();
// convert to our OfferJsonRow format
var jsonData = JsonConvert.DeserializeObject<Dictionary<string, List<OfferJsonRow>>>(jsonString);
var rawOfferData = jsonData["data"];
?
JSON:
Task<string>
这是我的代码:
{
"data": [{
"Latitude": "xxx",
"RedemptionType": "barcode",
"Version": "1",
"HeaderType": "",
"FontColour": "",
"LogoType": "",
"RedemptionLimit": "",
"RedemptionTimeMinutes": ""
}]
}
我相信如果我更换&#34;你好&#34;使用JSON然后它就是我想要的但是在将JSON粘贴到Visual Studio代码中时会抛出错误
答案 0 :(得分:0)
正如评论所暗示的那样,我最初只是为了让Visual Studio接受它而逃避JSON。例如:
var JSONString = ""Longitude"": ""xxx"",
""Latitude"": ""-xxx"",
""RedemptionType"": ""barcode"",
""Version"": ""1"",
""HeaderType"": """",
""FontColour"": """",
""LogoType"": """",
""RedemptionLimit"": """",
""RedemptionTimeMinutes"": """"
}]
}";
然后将它用作客户端代码所期望的json字符串,我不得不忘记它:
var unescapedjson = Regex.Unescape(JSONString);
对于Task<string>
,没有必要嘲笑HttpResponseMessage
所以不需要它。
我使用了真实的HttpResponseMessage
并将其content
字段手动设置为unescapedjson
:
var response = new HttpResponseMessage();
Microsoft.JScript.GlobalObject.unescape(JSONString);
response.Content = new StringContent(unescapedjson);
完整代码:
[Test]
public async Task AllPartners_HasPartners_GetsThem() {
var response = new HttpResponseMessage();
var unescapedjson = Regex.Unescape(JSONString);
var unescapedjson2 = Microsoft.JScript.GlobalObject.unescape(JSONString);
response.Content = new StringContent(unescapedjson);
await _offersDataService.ProcessResponse(response);
Assert.Greater(_offersDataService.AllPartners.Count, 0);
}