CSharp:JSON
From Aino Wiki
Contents
[hide]Librerie
NewtonSoft
Doc e Info Qui o scaricabile mediante NuGet
Serializzare \ Deserializzare
Dopo aver installato la libreria quindi aggiunta la reference 'Newtonsoft.Json'.
Data la classe:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | namespace EAI.WCF.Wrapper.Model { [DataContract] public class PollingModel { [DataMember] public Guid RecordGuid { get ; set ; } [DataMember] public Record RecordType { get ; set ; } } [DataContract] public class Record { [DataMember] public string TableName { get ; set ; } [DataMember] public Dictionary< string , string > KeyFields { get ; set ; } //= new Dictionary<string, string>(); public Record() { KeyFields = new Dictionary< string , string >(); } } } |
Serializzazione
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | using Newtonsoft.Json; // ... private void LogSoapMessagePayload(PollingModel model) { try { if ( bool .Parse(ConfigurationManager.AppSettings[ "BufferServiceLogEnabled" ])) { string serializedModel = JsonConvert.SerializeObject(model, Formatting.Indented); Logger.Info( string .Format( "INPUT FLOW -> BufferService, JSON: \r\n\"{0}\"" , serializedModel)); } } catch (Exception ex) { Logger.ErrorFormat( "[BufferService.svc.cs].LogSoapMessagePayload: {0}" , ex.Message); } } |
Deserializzazione
es 1. Classe
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | [DataContract] [Serializable] public class ItemMasterData { [DataMember(IsRequired = true )] public string divisionCode { get ; set ; } [DataMember(IsRequired = true )] public string companyCode { get ; set ; } [DataMember(IsRequired = true )] public string productCode { get ; set ; } [DataMember(IsRequired = true )] public productDescription productDescription { get ; set ; } [DataMember(IsRequired = true )] public Properties Properties { get ; set ; } [DataMember(IsRequired = true )] public ClassDescription ClassDescription { get ; set ; } [DataMember(IsRequired = true )] public SubclassDescription SubclassDescription { get ; set ; } [DataMember] public string catalogueItemCode { get ; set ; } [DataMember] public string statusCode { get ; set ; } // ... etc } |
Metodo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | private void LogSoapMessagePayload(ItemMasterData model) { try { if ( bool .Parse(ConfigurationManager.AppSettings[ "EAIItemMasterLogEnabled" ])) { string serializedModel = JsonConvert.SerializeObject(model, Formatting.Indented); Logger.Info( string .Format( "INPUT FLOW -> ItemMaster, JSON: \r\n\"{0}\"" , serializedModel)); } } catch (Exception ex) { Logger.ErrorFormat( "[EAIItemMaster.svc.cs].LogSoapMessagePayload: {0}" , ex.Message); } } |
es 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | using System.Configuration; using System.IO; using System.IO; using Newtonsoft.Json; // ... private static BusinessCustomerServiceReference.BusinessCustomer GetBC_FromFile() { BusinessCustomerServiceReference.BusinessCustomer model = null ; OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = ConfigurationManager.AppSettings[ "SOAPLoggerPath" ] + "Json" ; // "C:\\spindox_logs\\xml da Pasquale"; if (ofd.ShowDialog() == DialogResult.OK) { if (ofd.FileName.EndsWith( ".json" )) { FileStream fs = new FileStream(ofd.FileName, FileMode.Open); StreamReader sr = new StreamReader(fs); model = JsonConvert.DeserializeObject<BusinessCustomerServiceReference.BusinessCustomer>(sr.ReadToEnd()); sr.Close(); fs.Close(); } else { XmlSerializer s = new XmlSerializer( typeof (BusinessCustomerServiceReference.BusinessCustomer)); FileStream fs = new FileStream(ofd.FileName, FileMode.Open); model = (BusinessCustomerServiceReference.BusinessCustomer)s.Deserialize(fs); fs.Close(); } } return model; } |
Mappa e Link
JSON | Visual Studio | MS SQL | Dizionario
Parole chiave: