Login Login
MORE

WIDGETS

Widgets

Wanted articles
Who is online?
Article tools

CSharp:JSON

From Aino Wiki

Jump to: navigation, search

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:

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

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

    [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:

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.

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


C#


JSON | Visual Studio | MS SQL | Dizionario


Parole chiave:

Author