0

現在、サードパーティのAPIと統合しています。サードパーティは、次の形式のjson文字列を受け入れています。

{    
"test-specialism": null,
"salaryCollection": [
    {
      "id":
               {
                   "jobtypeid": 1
               },  
"maxsalary": 564,
        "minsalary": 123,
        "salarycurrency": "GBP",
        "salarytype": "A"
    },
{
        "id":
               {
                   "jobtypeid": 2
               },
               "maxsalary": null,
               "minsalary": null,
               "salarycurrency": "GBP",
               "salarytype": null
           },
    }],

}

そして、これが私のオブジェクトです:

   public class Salary {
    public double Minimum { get; set; }
    public double Maximum { get; set; }
    public PaymentFrequency Frequency { get; set; }
    public double Step { get; set; }
    public int JobTypeId { get; set; }
    public SalarySetting() {        }

}

public class Alert {
   public string Specialism {get;set;}
   public Salary Permanent { get; set; }
   public Salary Temporary { get; set; }
   public Salary Contract { get; set; }
}

ご覧のとおり、オブジェクト構造はJSON構造と非常に矛盾しています。オブジェクトをその指定されたjson文字列に変換する理想的な方法は何でしょうか?JSONPropertyを使用してプロパティをマップしようとしましたが、この場合はうまく機能しないようです。

4

2 に答える 2

2
  1. jsonを説明するクラスを作成する
  2. AutoMapperを使用して、クラスからjson関連のクラスにマップします。
于 2012-08-02T08:28:51.407 に答える
0

System.Web.Script.Serialization.JavaScriptSerializerを使用できます。例 :

public class Product{
    public string Name { get; set; }
    public string ID { get; set; }   
}

そして、いくつかのインスタンスを Lists に追加します:

Product p1 = new Product {Name="Product1",ID="1"};
Product p2 = new Product {Name="Product2",ID="2"};
List<Product> pList = new List<Product>() {p1,p2};

次に、それをシリアル化します。

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string sJSON = serializer.Serialize(pList);
于 2012-08-02T08:29:55.197 に答える