2015-03-05 6 views
12

मैं एक स्ट्रिंग है:Convert स्ट्रिंग, का उपयोग Json.Net

[ 
    { 
    "key": "key1", 
    "value": "{'Time':'15:18:42','Data':'15:18:42'}", 
    "duration": 5 
    }, 
    { 
    "key": "key1", 
    "value": "{'Time':'15:18:42','Data':'15:18:42'}", 
    "duration": 5 
    } 
] 

मॉडल में मेरी कक्षा:

public class CPacket 
{ 
    public string key { get; set; } 
    public string value { get; set; } 
    public int duration { get; set; } 
} 

मैं Json.Net उपयोग करते हैं, मैं स्ट्रिंग परिवर्तित करना चाहते हैं जेसन ओजेक्ट को झुकाओ।

CPacket c = JsonConvert.DeserializeObject<CPacket>(strPostData); 

लेकिन यह त्रुटि:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'QuoteAPI.Models.CPacket' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

उत्तर

27

आपका JSON CPacket वस्तुओं, न सिर्फ एक वस्तु की एक सरणी का प्रतिनिधित्व करता है। आपको एक सूची में deserialize करने की जरूरत है।

List<CPacket> list = JsonConvert.DeserializeObject<List<CPacket>>(strPostData); 
+0

ओह! आपका बहुत बहुत धन्यवाद! इसने काम कर दिया! :) –

-9
{ 
     var url = "http://jsonplaceholder.typicode.com/posts"; 

     var webClient = new WebClient(); 
     var responseStr = webClient.DownloadString(url); 

     //JObject jResponse = JObject.Parse(responseStr); 
     JArray jArray = JArray.Parse(responseStr); 


     List<User> userList = new List<User>(); 

     foreach (var item in jArray) 
     { 
      User user = new User(); 

      user.UserID = Convert.ToInt32(item["userId"]); 
      user.ID = Convert.ToInt32(item["id"]); 
      user.Title = Convert.ToString(item["title"]); 
      user.Body = Convert.ToString(item["body"]); 

      userList.Add(user); 
     } 







public partial class Search : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     string url = "http://jsonplaceholder.typicode.com/posts"; 
     var webClient = new WebClient(); 
     var responseStr = webClient.DownloadString(url); 
     JArray jArray = JArray.Parse(responseStr); 



     List<Class1> userList = new List<Class1>(); 

     foreach (var item in jArray) 
     { 
      Class1 user = new Class1(); 


      user.userId = Convert.ToInt32(item["userId"]); 
      user.id = Convert.ToInt32(item["id"]); 
      user.title = Convert.ToString(item["title"]); 
      user.body = Convert.ToString(item["body"]); 

      userList.Add(user); 
     } 
     Repeater1.DataSource = userList; 
     Repeater1.DataBind(); 
     var myClass = _download_serialized_json_data<List<Class1>>(url); 
     WebRequest request = WebRequest.Create(url); 
     WebResponse ws = request.GetResponse(); 
     //Stream stream1 = response.GetResponseStream(); 
     //StreamReader sr = new StreamReader(stream1); 
     //string strsb = sr.ReadToEnd(); 
     //object objResponse = JsonConvert.DeserializeObject(strsb, JSONResponseType); 
     DataContractJsonSerializer jsonSerializer = 
       new DataContractJsonSerializer(typeof(List<Class1>)); 
     object objResponse = jsonSerializer.ReadObject(ws.GetResponseStream()); 
     List<Class1> jsonResponse 
     = objResponse as List<Class1>; 
     List<Class1> photos = (List<Class1>)jsonSerializer.ReadObject(ws.GetResponseStream()); 
    } 

    private static T _download_serialized_json_data<T>(string url) where T : new() 
    { 
     using (var w = new WebClient()) 
     { 
      var json_data = string.Empty; 
      // attempt to download JSON data as a string 
      try 
      { 
       json_data = w.DownloadString(url); 
      } 
      catch (Exception) { } 
      // if string with JSON data is not empty, deserialize it to class and return its instance 
      return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject<T>(json_data) : new T(); 
     } 
    } 
} 



} 
+6

कृपया कोड की दीवार पोस्ट करने के बजाय अपने उत्तर पर विस्तृत करें। –