Newtonsoft.Json.JsonWriter

[一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/4754769.html

JsonWriter使用:

前台

 1 $.post("Ajax_EstimatedIncome.aspx/GetEstimatedIncomePlus", { ProjectID: m_EstimatedIncomeTEN.ProjectID }, function (json) {
 2                 var m = eval('(' + json + ')');
 3                 var m_ProjectTEN = eval('(' + m.ProjectTEN + ')');
 4                 var m_ProjectPlugTEN = eval('(' + m.ProjectPlugTEN + ')');
 5                 var m_CustomersTEN = eval('(' + m.CustomersTEN + ')');
 6                 $('#form1 input[name="Stock"]').val(m.Stock);
 7                 $('#form1 input[name="EstimatedID"]').val(m_EstimatedIncomeTEN.EstimatedID);
 8                 $('#form1 input[name="BluntMonney"]').val(m_EstimatedIncomeTEN.BluntMonney);
 9                 $('#form1 input[name="BluntMonneyA"]').val(m_EstimatedIncomeTEN.BluntMonneyA);
10                 $('#form1 input[name="CreateDate"]').val(CreateDate.getFullYear() + "-" + (CreateDate.getMonth() + 1) + "-" + CreateDate.getDate());
11                 $('#form1 input[name="CustomersName"]').val(m_CustomersTEN.ShortName);
12                 $('#form1 input[name="DesignFee"]').val(m_ProjectTEN.DesignFee);
13                 $('#form1 input[name="EstimatedCost"]').val(m_EstimatedIncomeTEN.EstimatedCost);
14                 $('#form1 input[name="EstimatedDate"]').val(EstimatedDate.getFullYear() + "-" + (EstimatedDate.getMonth() + 1));
15                 $('#form1 input[name="EstimatedMonney"]').val(m_EstimatedIncomeTEN.EstimatedMonney);
16                 $('#form1 input[name="EstimatedMonneyA"]').val(m_EstimatedIncomeTEN.EstimatedMonneyA);
17                 $('#form1 input[name="EstimatedRatio"]').val(m_EstimatedIncomeTEN.EstimatedRatio);
18                 $('#form1 input[name="MyProjectNo"]').val(m_ProjectTEN.MyProjectNo);
19                 $('#form1 input[name="ProjectName"]').val(m_ProjectTEN.ProjectName);
20                 $('#form1 input[name="ProjectNum"]').val(m_ProjectTEN.ProjectNum);
21                 $('#form1 input[name="BluntDate"]').val(EstimatedDate.getFullYear() + "-" + (EstimatedDate.getMonth() + 1));
22                 $('#form1 input[name="Remark"]').val(m_EstimatedIncomeTEN.Remark);
23                 $('#form1 input[name="EstimatedRatio"]').val(m_EstimatedIncomeTEN.EstimatedRatio);
24                 $('#form1 input[name="UserName"]').val(m_EstimatedIncomeTEN.UserName);
25                 $('#form1 input[name="CreaterID"]').val(m_EstimatedIncomeTEN.UserID);
26                 if (m_ProjectPlugTEN.ProjectRange == 0)
27                     $('#form1 input[name=ProjectRange]').val('自营');
28                 else if (m_ProjectPlugTEN.ProjectRange == 1)
29                     $('#form1 input[name=ProjectRange]').val('分包');
30                 else if (m_ProjectPlugTEN.ProjectRange == 2)
31                     $('#form1 input[name=ProjectRange]').val('代理');
32                 $('#form1 input[name="ProjectID"]').val(m_EstimatedIncomeTEN.ProjectID);
33             });

后台

 1 [WebMethod(EnableSession = true)]
 2     public string GetEstimatedIncomePlus()
 3     {
 4         string ProjectID = Request.Params["ProjectID"] == null ? "0" : Request.Params["ProjectID"].ToString();
 5         try
 6         {
 7             m_ProjectTEN = bll_ProjectTEO.GetModel(long.Parse(ProjectID));
 8             m_ProjectPlugTEN = bll_ProjectPlugTEO.GetModel(m_ProjectTEN.ProjectPlugID);
 9             m_CustomersTEN = bll_CustomersTEO.GetModel(m_ProjectPlugTEN.CustomersID.Value);
10             string Stock = "0";
11             DataTable dt_ProjectSum = bll_ProjectSumTEO.GetList_Detail("ProjectID=" + ProjectID);
12             if (dt_ProjectSum != null && dt_ProjectSum.Rows.Count > 0)
13                 Stock = dt_ProjectSum.Rows[0]["Stock"].ToString();
14             else
15                 Stock = Global.Precision;
16             string UserName = string.Empty;
17             if (ND.Webs.Outsourcing.PlatBase.Business.PlatBaseUserTBO.LoginInfo != null)
18                 UserName = ND.Webs.Outsourcing.PlatBase.Business.PlatBaseUserTBO.LoginInfo.UserName;
19             int UserID = 0;
20             if (ND.Webs.Outsourcing.PlatBase.Business.PlatBaseUserTBO.LoginInfo != null)
21                 UserID=ND.Webs.Outsourcing.PlatBase.Business.PlatBaseUserTBO.LoginInfo.UserID;
22             StringWriter sw = new StringWriter();
23             using (JsonWriter writer = new JsonTextWriter(sw))
24             {
25                 writer.Formatting = Formatting.Indented;
26                 writer.WriteStartObject();
27                 writer.WritePropertyName("ProjectTEN");
28                 writer.WriteValue(JsonConvert.SerializeObject(m_ProjectTEN));
29                 writer.WritePropertyName("ProjectPlugTEN");
30                 writer.WriteValue(JsonConvert.SerializeObject(m_ProjectPlugTEN));
31                 writer.WritePropertyName("CustomersTEN");
32                 writer.WriteValue(JsonConvert.SerializeObject(m_CustomersTEN));
33                 writer.WritePropertyName("Stock");
34                 writer.WriteValue(Stock);
35                 writer.WritePropertyName("UserName");
36                 writer.WriteValue(UserName);
37                 writer.WritePropertyName("UserID");
38                 writer.WriteValue(UserID);
39                 writer.WriteEndObject();
40                 writer.Flush();
41             }
42             sw.Close();
43             return sw.GetStringBuilder().ToString();
44         }
45         catch (Exception ex)
46         {
47             return "Error";
48         }
49     }

 

转载于:https://www.cnblogs.com/wanghafan/p/4754769.html

Newtonsoft.Json是一个流行的.NET JSON框架,用于在JSON格式和.NET对象之间进行序列化和反序列化。要序列化类中的私有字段,通常需要使用一些额外的技巧,因为默认情况下,Newtonsoft.Json不会序列化私有字段。 一种方法是使用自定义的`JsonConverter`,这个转换器可以让你控制序列化和反序列化的过程。以下是一个简单的自定义`JsonConverter`示例,它允许序列化私有字段: ```csharp using Newtonsoft.Json; using System; using System.Reflection; public class PrivateFieldConverter : JsonConverter { public override bool CanConvert(Type objectType) { return true; } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException("反序列化私有字段不被支持"); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var type = value.GetType(); writer.WriteStartObject(); foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)) { object obj = fieldInfo.GetValue(value); var attribute = fieldInfo.GetCustomAttribute<JsonPropertyAttribute>(); var name = attribute != null ? attribute.PropertyName : fieldInfo.Name; serializer.Serialize(writer, name, obj); } writer.WriteEndObject(); } } public class MyObject { [JsonProperty] private int privateField; } ``` 在这个示例中,`PrivateFieldConverter`类覆盖了`WriteJson`方法来遍历所有非公共字段,并使用`JsonSerializer`来序列化它们。`MyObject`类中的私有字段`privateField`通过`JsonProperty`属性标记,以便`PrivateFieldConverter`能够找到并序列化它。 要使用这个转换器,你需要在序列化时指定它: ```csharp var settings = new JsonSerializerSettings { Converters = new List<JsonConverter> { new PrivateFieldConverter() } }; var obj = new MyObject { privateField = 42 }; string json = JsonConvert.SerializeObject(obj, settings); ``` 这样,即使是私有字段也会被包含在序列化的JSON字符串中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值