C#:JSON嵌套对象反系列化,读取对象的值

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hdhai9451/article/details/50039829
//引用命名空间
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


static void Main(string[] args)
{
    Console.WriteLine("try: ");
	string value = "Hardness France"; //调用百度,返回JSON格式

	string info = TranslationToFra(value);
	//info结果:"{\"from\":\"en\",\"to\":\"fra\",\"trans_result\":[{\"src\":\"Hardness France\",\"dst\":\"la duret\\u00e9 de la france\"}]}\r\n"

	info = info.Replace(":[{", ":{").Replace("}]}", "}}"); //要过滤掉中括号
	ObjectJson model = (ObjectJson)JsonConvert.DeserializeObject<ObjectJson>(info);
	JObject jsonObj = JObject.Parse(info);

	////方法一:
    //string from=jsonObj["from"].ToString();
    //string dst=((JObject )jsonObj["trans_result"])["dst"].ToString();

	//方法二:
    if (!string.IsNullOrWhiteSpace(model.error_code))
    {
        Console.WriteLine(model.error_msg);
    }
    else
    {
        Console.WriteLine(model.trans_result.dst);
    }            

    Console.ReadKey();
}



public class ObjectJson
{
    public string from { get; set; }
    public string to { get; set; }
    public string error_code { get; set; }
    public string error_msg { get; set; }
    public SubObject trans_result { get; set; }
}
public class SubObject
{
    public string src { get; set; }
    public string dst { get; set; }

}

展开阅读全文

XmlSerializer反系列化问题

10-04

在三层架构中,客户端按照XmlSerializer方式系列化了一个实体类,然后通过TCP发送到服务端,同时也发送了实体类的文件名、命名空间、Class名称等信息rn由于实体类涉及Class比较多,不想一个个罗列,希望服务端通过反射形式,对这个XML进行反系列化rnrn问题就是:rn当客户端告知服务端程序当前的XML文件是SocketListening.Entity.T_SizeType这个Class的情况下,如何通过反射模式进行反系列化?rnrnrn其中的一个实体类如下:rnrn[code=C#]rnusing System;rnusing System.Collections.Generic;rnusing System.Text;rnrnnamespace SocketListening.Entityrnrn #region T_SizeType的存储过程实体类代码rnrn public class T_SizeTypern rn private int m_intSizeTypeCTN = 0;rn private string m_strSizeType = "";rn private string m_strSizeCTN = "";rn private string m_strTypeCTN = "";rn private string m_strWhereCondition = "";rn private string m_strOrderByExpression = "";rnrn public int SizeTypeCTNrn rn get return m_intSizeTypeCTN; rn set m_intSizeTypeCTN = value; rn rn public string SizeTypern rn get return m_strSizeType; rn set m_strSizeType = value; rn rn public string SizeCTNrn rn get return m_strSizeCTN; rn set m_strSizeCTN = value; rn rn public string TypeCTNrn rn get return m_strTypeCTN; rn set m_strTypeCTN = value; rn rn public string WhereConditionrn rn get return m_strWhereCondition; rn set m_strWhereCondition = value; rn rn public string OrderByExpressionrn rn get return m_strOrderByExpression; rn set m_strOrderByExpression = value; rn rnrn rn #endregionrnrnrn[/code]rnrnrn系列化反系列化如下:rn[code=C#]rn public string XmlSerializeMS(T t)rn rn tryrn rn String mXmlString = null;rn System.IO.MemoryStream mMS = new System.IO.MemoryStream();rnrn System.Xml.Serialization.XmlSerializer mXS = new System.Xml.Serialization.XmlSerializer(typeof(T));rnrn mXS.Serialize(mMS, t);rnrn mXmlString = Convert.ToBase64String(mMS.ToArray());rnrn return mXmlString;rn rn catch (NullReferenceException NullEx)rn rn throw NullEx;rn rn catch (Exception Ex)rn rn throw Ex;rn rn rnrn public T XmlDeserializeMS(string pXmlString)rn rn tryrn rn System.Xml.Serialization.XmlSerializer mXS = new System.Xml.Serialization.XmlSerializer(typeof(T));rnrn System.IO.MemoryStream mMS = new System.IO.MemoryStream(Convert.FromBase64String(pXmlString));rnrn return (T)mXS.Deserialize(mMS);rn rn catch (NullReferenceException NullEx)rn rn throw NullEx;rn rn catch (Exception Ex)rn rn throw Ex;rn rn rnrn[/code]rnrn系列化调用代码:rn[code=C#]rnstring mEntityXML = "";rnSocketListening.Entity.T_SizeType mEST = new SocketListening.Entity.T_SizeType();rnrnmEntityXML = XmlSerializeMS(mEST);rnrn[/code]rn 论坛

没有更多推荐了,返回首页