2012-11-07 6 views
55

मैं समझता हूं कि एएसपी.नेट वेब एपीआई मूल रूप से (डी) ऑब्जेक्ट्स को क्रमबद्ध करने के लिए जेसन.NET का उपयोग करता है, लेकिन क्या JsonSerializerSettings ऑब्जेक्ट निर्दिष्ट करने का कोई तरीका है जिसे आप उपयोग करना चाहते हैं?एमवीसी 4 वेब एपीआई में जेसन.नेट के लिए कस्टम जेसनएसरियललाइज़र सेटिंग्स कैसे सेट करें?

उदाहरण के लिए, यदि मैं type जानकारी को क्रमबद्ध JSON स्ट्रिंग में शामिल करना चाहता हूं तो क्या होगा? आम तौर पर मैं .Serialize() कॉल में सेटिंग्स इंजेक्ट करता हूं, लेकिन वेब एपीआई चुपचाप करता है। मुझे मैन्युअल रूप से सेटिंग्स इंजेक्ट करने का कोई तरीका नहीं मिल रहा है।

उत्तर

97

में Formatters.JsonFormatter.SerializerSettings संपत्ति का उपयोग करके आप JsonSerializerSettings को कस्टमाइज़ कर सकते हैं।

उदाहरण के लिए, तुम कर सकते हो कि विधि Application_Start() में:

protected void Application_Start() 
{ 
    HttpConfiguration config = GlobalConfiguration.Configuration; 
    config.Formatters.JsonFormatter.SerializerSettings.Formatting = 
     Newtonsoft.Json.Formatting.Indented; 
} 
+30

आप नियंत्रक या कार्रवाई प्रति ऐसा करने के कर सकते हैं? – Chazt3n

+1

मैं इसे एएसपी.NET ऐप में हैंगफायर स्थापित करने के लिए काम नहीं कर सकता। यह गलत लिबररी या कुछ का संदर्भ दे रहा है। डिफ़ॉल्ट सेटिंग्स के साथ दूसरे उत्तर का उपयोग करना पड़ा .. – ppumkin

31

आप प्रत्येक JsonConvert के लिए JsonSerializerSettings निर्दिष्ट कर सकते हैं, और आप एक वैश्विक डिफ़ॉल्ट सेट कर सकते हैं।

एकल JsonConvert एक अधिभार के साथ:

// Option #1. 
JsonSerializerSettings config = new JsonSerializerSettings { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore }; 
this.json = JsonConvert.SerializeObject(YourObject, Formatting.Indented, config); 

// Option #2 (inline). 
JsonConvert.SerializeObject(YourObject, Formatting.Indented, 
    new JsonSerializerSettings() { 
     ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore 
    } 
); 

वैश्विक Global.asax.cs में Application_Start() में कोड के साथ स्थापना:

JsonConvert.DefaultSettings =() => new JsonSerializerSettings { 
    Formatting = Newtonsoft.Json.Formatting.Indented, 
    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore 
}; 

संदर्भ: https://github.com/JamesNK/Newtonsoft.Json/issues/78

+3

एफडब्ल्यूआईडब्ल्यू, दूसरी विधि जो मैंने शुरू में कोशिश की थी, जो * काम * नहीं किया था। मुझे 'कार्लोफिगिरा के उत्तर] (http://stackoverflow.com/a/13274791/44853) में' HttpConfiguration' का उपयोग करना था क्योंकि 'JsonConvert.DefaultSettings' में कॉन्फ़िगर की गई सेटिंग्स को नहीं देखा जा रहा था। –

+1

मेरे मामले में ग्लोबल सेटिंग usgin 'JsonSerializerSettings' है जो मेरे लिए काम करता है। मुझे काम करने के लिए HttpCOnfiguration नहीं मिल सका, यह एक और असेंबली विधियों (हैंगफ्रे) के साथ वापस आ रहा था यकीन नहीं क्यों। – ppumkin

+0

क्या मैं छिपे हुए फ़ील्ड 'formHiddenField.Value = JsonConvert.SerializeObject (listaCursos, Formatting.Indented, jsonSerializerSettings) का उपयोग कर सकता हूं;' और 'var data = $ (' # formHiddenField ') मान प्राप्त करने के लिए JQuery का उपयोग करें। Val(); '? – Kiquenet

2

जवाब है ओ 2 जोड़ना ओ Global.asax.cs Application_Start विधि के लिए च कोड

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; 
json.SerializerSettings.PreserveReferencesHandling = 
    Newtonsoft.Json.PreserveReferencesHandling.All; 

संदर्भ: Handling Circular Object References

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