2014-04-09 31 views
0

我要輸出在JSON以下示例數據:轉換JSON屬性爲數字使用Json.net序列化時和C#

{ 「SOURCE_ID」: 「1234」, 「接觸」:[ 「15」:」 020 8111 1111" ]}

我已經使用以下代碼來實現這一點使用Json.net嘗試:

using Newtonsoft.Json; 
using Newtonsoft.Json.Linq; 

namespace Test.Utility 
{ 
    public class RequestHelper 
    { 
     internal static string CreateData(string sourceId, string telephone) 
     { 
      JArray createContact = new ContactFieldHelper 
      { 
       Phone = new JValue(telephone) 
      }; 

      var createData = new DataFieldHelper 
      { 
       SourceId = sourceId, 
       CreateContact = createContact 
      }; 

      string output = JsonConvert.SerializeObject(createData); 
      return output; 
     } 

    } 

    class DataFieldHelper 
    { 
     [JsonProperty(PropertyName = "source_id")] 
     public string SourceId { get; set; } 

     [JsonProperty(PropertyName = "contacts")] 
     public JArray CreateContact { get; set; } 
    } 

    public class ContactFieldHelper : JArray 
    { 
     [JsonProperty(PropertyName = "15")] 
     public JValue Phone { get; set; } 
    } 
} 

然而,這是顯示用於一個空白輸出‘聯繫人’字段。它適用於僅使用值的情況,但不適用於JArray中存在關鍵值對的情況。因此,例如,以下將輸出:

{ 「SOURCE_ID」: 「1234」, 「人脈」: 「020 8111 1111」]}

using Newtonsoft.Json; 
using Newtonsoft.Json.Linq; 

namespace Test.Utility 
{ 
    public class RequestHelper 
    { 
     internal static string CreateData(string sourceId, string telephone) 
     { 
      JArray createContact = new JArray 
            { 
             new JValue(telephone) 
            }; 

      var createData = new DataFieldHelper 
      { 
       SourceId = sourceId, 
       CreateContact = createContact 
      }; 

      string output = JsonConvert.SerializeObject(createData); 
      return output; 
     } 

    } 

    class DataFieldHelper 
    { 
     [JsonProperty(PropertyName = "source_id")] 
     public string SourceId { get; set; } 

     [JsonProperty(PropertyName = "contacts")] 
     public JArray CreateContact { get; set; } 
    } 
} 

任何想法?

回答

0

的簡單梅託德在我個人的意見才達到你的目標是:

創建JSON字符串(照顧到每一個參數,數組名等)(看看這裏json examples這裏看看驗證您的JSON(這是無效的,因爲它是wwrite)json validator

轉到這裏http://json2csharp.com/,並使用此工具你的新類轉換你的JSON在簡單的類

導入到你的項目,然後序列化和幾排反序列化與類System.Web.Scrip t.Serialization.Jasixserializer

它會更簡單快捷。

在例如,從您的代碼:

{"source_id":"1234","contacts":["15":"020 8111 1111"]}(invalid json it gives error on validation) 

類派生:

public class Contact 
{ 
    public string __invalid_name__15 { get; set; } 
} 

public class RootObject 
{ 
    public string source_id { get; set; } 
    public List<Contact> contacts { get; set; } 
} 
,如果你願意,你可以裝飾你的財產或者序列化的屬性,整個類,然後 只需使用javascriptserializar喜歡這裏

protected void SerializeItem(string s){ 

System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); 

RootObject rt = js.Deserialize<RootObject>(s);//deserialization from string to object 
string S = js.Serialize(RootObject);//serialization from object to complex json string 

} 

要更清潔ic掛一點點你的JSON代碼ADN這裏派生類

public class Contact 
{ 
public string id { get; set; } 
public string phone { get; set; } 
} 

public class RootObject 
{ 
public string sourceid { get; set; } 
public List<Contact> contacts { get; set; } 
} 

這裏JSON重新例如:

{"sourceid": "1234", 
     "contacts": [ 
         { 
         "id": "15", 
         "phone":"020 8111 1111" 
         } 
        ] 
    } 
相關問題