2

メンバーのデータ型Dictionaryの1つを持つオブジェクトは、JSONを介して送信するときにnullとして取得されています

次のスキーマで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を使用してJSONをAPIに送信しようとしました

  $.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);

                       });







        }

    });
4

2 に答える 2

3

jsonは次のようにフォーマットする必要があります。ValueおよびKeysプロパティ名は使用しないでください。また、プロパティ名は引用符で囲んでください。

{"APIKey":"test","APISecret":"test","APIVersion":"1.0",
"Fields":{"JobID":"2290277","CountryID":"1"}}

これらの問題が発生するたびに、いくつかの簡単なコードを実行して、ServiceStackSerializerが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と比較します。

于 2013-03-13T13:51:27.670 に答える
0

jScriptを変更する必要があります

{"APIKey": "test"、 "APISecret": "test"、 "APIVersion": "1.0"、 "Fields":" {'JobID': '2290277'、'CountryID': '1'} " }

于 2013-03-14T01:48:34.830 に答える