2015-12-22 10 views
6

तो, यहाँ मैं JsonResult jr है, यह इस तरह दिखता है, जबकि प्रकार का एक पहला मौका अपवाद 'Newtonsoft.Json.JsonReaderException' हुई (उत्पादन एक ही लाइन है, मैं इसे यहाँ से प्रारूप):C# वस्तु को आंशिक json परिवर्तित

public class User 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Id { get; set; } 
    public string Office { get; set; } 
    public string Email { get; set; } 
    public string Code { get; set; } 
} 

मैं deserializing आंशिक JSON टुकड़े का उपयोग करने के लिए अपने ऑब्जेक्ट के लिए json मैप करने के लिए कोशिश कर रहा हूँ: http://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm

string jr = {"Results": 
    [ 
     {"Code":"DEMO", 
     "Id":"1285", 
     "Office":"9881", 
     "Customers": 
      [ 
      2713, 
      94204 
      ], 
     "Account":196, 
     "Appointments": 
      [ 
      14, 
      58 
      ], 
     "Role":0, 
     "UserName":"demo", 
     "UserId":3669, 
     "FirstName":"Peter", 
     "LastName":"Pan", 
     "Phones": 
      [ 
      "(888) 888-8888" 
      ], 
     "Fax":null, 
     "Email":"[email protected]", 
     "SMS":null, 
     "RecordStatus":"1"}, 


     {"Code":"DEMO", 
     "Id":"9292", 
     "Office":"9881", 
     "Customers": 
      [ 
      13, 
      904 
      ], 
     "Account":196, 
     "Appointments": 
      [ 
      14, 
      58 
      ], 
     "Role":0, 
     "UserName":"berry", 
     "UserId":302, 
     "FirstName":"Jimmy", 
     "LastName":"White", 
     "Phones": 
      [ 
      "(888) 888-8888" 
      ], 
     "Email":"[email protected]", 
     "SMS":null, 
     "RecordStatus":"1"} 
    ], 
"TotalResults":2, 
"MilliSeconds":4} 

यहाँ मेरी वस्तु User है

मैंने उदाहरण का पालन किया, लेकिन मुझे एक त्रुटि मिली: Newtonsoft.Json.dll में 'Newtonsoft.Json.JsonReaderException' प्रकार का पहला मौका अपवाद हुआ।

कई लोग ऑनलाइन यह कहते हैं कि यह खराब जेसन के कारण है। मैंने अपनी जांच की, मुझे कुछ भी गलत नहीं मिला।

JObject response = JObject.Parse(jr); 
IList<JToken> results = response["Results"].Children().ToList(); 

IList<User> searchResults = new List<User>(); 
foreach(JToken result in results) 
{ 
    System.Diagnostics.Debug.WriteLine(result); //just to check my json data. 
    User searchResult = JsonConvert.DeserializeObject<User>(results.ToString()); //get exception on this line. 
    searchResults.Add(searchResult); 
} 

पहले result उत्पादन इस तरह दिखता है:

{ 
     "Code":"DEMO", 
     "Id":"1285", 
     "Office":"9881", 
     "Customers": 
      [ 
      2713, 
      94204 
      ], 
     "Account":196, 
     "Appointments": 
      [ 
      14, 
      58 
      ], 
     "Role":0, 
     "UserName":"demo", 
     "UserId":3669, 
     "FirstName":"Peter", 
     "LastName":"Pan", 
     "Phones": 
      [ 
      "(888) 888-8888" 
      ], 
     "Fax":null, 
     "Email":"[email protected]", 
     "SMS":null, 
     "RecordStatus":"1" 
} 
नहीं

यकीन है कि क्यों यह अपवाद है, होता है सोच रहा है कि यह कैसे तय करने के लिए ..

+0

आप की जांच की है Json.Net और अपने फोन मूल्यों के लिए एक कस्टम serializer? – doogle

+0

नहीं। मैं सी # के लिए नया हूं, आपको यकीन नहीं है कि आपका क्या मतलब है .. –

+1

http://www.newtonsoft.com/json .NET के लिए JSON लाइब्रेरी पर जाना है, यह वही कर सकता है जो आप चाहते हैं आपको अपने 'उपयोगकर्ता' वर्ग में कुछ विशेषताओं को जोड़ना होगा ताकि धारावाहिक को पता चले कि कौन से फ़ील्ड कहां जाते हैं। – doogle

उत्तर

4

इस पंक्ति में: नीचे मेरी कोड है

User searchResult = JsonConvert.DeserializeObject<User>(result.ToString()) // result and not results 

आप एक साधारण परिणाम deserialize करना चाहते हैं और परिणाम नहीं।

पूर्ण कोड:

JObject response = JObject.Parse(jr); 
IList<JToken> results = response["Results"].Children().ToList(); 

IList<User> searchResults = new List<User>(); 
foreach (JToken result in results) 
{ 
    System.Diagnostics.Debug.WriteLine(result); //just to check my json data. 
    User searchResult = JsonConvert.DeserializeObject<User>(result.ToString()); //get exception on this line. 
    searchResults.Add(searchResult); 
} 

BTW, आप कुछ बुनियादी Linq के साथ पाश की जगह ले सकता:

JObject response = JObject.Parse(jr); 
IList<User> searchRes = response["Results"].Select(r => JsonConvert.DeserializeObject<User>(r.ToString())).ToList(); 
+1

ओह .... मैं बहुत लापरवाह हूँ .. हाँ यही कारण है कि, यह अब काम करता है। आखिरकार। और मेरे लूप को बदलने के लिए धन्यवाद। अब यह बहुत अच्छा है। :) –

+0

मैं बस का कहना है कि जिस तरह से करने के लिए ** अपने स्वयं वस्तु ** को json का ही हिस्सा कन्वर्ट चाहते यहाँ है: [http://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm ] –

संबंधित मुद्दे