ServiceStack字典DTO字典<字符串,字符串>数据类型的反序列化
问题描述:
通过JSONServiceStack字典DTO字典<字符串,字符串>数据类型的反序列化
我已经创建了下面的模式发送DTO当与所述构件的数据类型字典中的一个对象被检索为空
public class myclass
{
public string APIKey {get;set;}
public string APISecret {get;set;}
public string APIVersion {get;set;}
public Dictionary<string,string> Fields {get;set;}
}
我试着用下面的JS
$.ajax({
type: "GET",
dataType: "jsonp",
url: $('#txtUrl').val(),
data: {APIKey:"test",APISecret:"test", APIVersion:"1.0",Fields:[{"Key":"JobID","Value":"2290277"},{"Key":"CountryID","Value":"1"}]},
success: function (data) {
// Now the two will work
$.each(data, function(key, value) {
if (key == 'Fields')
{
$.each(value, function(key2, value2) {
$('#result').append('<br />' + key2 + ' ' + value2);
});
}
else
$('#result').append('<br />' + key + ' ' + value);
});
}
});
答
需要修改我的JScript
{ “APIKey”: “测试”, “APISecret”: “测试”, “APIVersion”: “1.0”, “字段”:“ { '作业ID' :'2290277','CountryID':'1'} “}
答
你的JSON发送JSON我API应该像这样被格式化。不要使用值和键属性名称。也把你的财产名称引号。
{"APIKey":"test","APISecret":"test","APIVersion":"1.0",
"Fields":{"JobID":"2290277","CountryID":"1"}}
每当我碰到这些问题,我运行一些简单的代码来看看ServiceStack串行如何预计JSON进行格式化:
var m = new myclass();
m.APIKey = "test";
m.APISecret = "test";
m.APIVersion = "1.0";
m.Fields =new Dictionary<string, string>();
m.Fields.Add("JobID", "2290277");
m.Fields.Add("CountryID", "1");
string json = m.ToJson();
然后比较,为你自己的JSON。
我以不同的方式解决了这个问题。需要将JScript修改为{“APIKey”:“test”,“APISecret”:“test”,“APIVersion”:“1.0”, “Fields”:“{'JobID':'2290277','CountryID ':' 1' }“}。查看区别?字段值中的双引号 – 2013-03-14 01:45:46
您不需要混淆双引号和单引号。它更简单的使用大括号的对象。有关json规范,请参阅http://www.json.org/。 – kampsj 2013-03-14 13:20:55