简体   繁体   English

在C#中将属性附加到序列化的json字符串

[英]append property to serialized json string in c#

I have a class which i want to serialize 我有一个要序列化的课程

public class DesignEntity
{
    public string DesignName { get; set; }
    public string DesignID { get; set; }
    [ScriptIgnore]
    public string designDetails { get; set; }
}

property designDetails already contains serialized data so no need to serialized it again 属性designDetails已经包含序列化的数据,因此无需再次对其进行序列化

DesignEntity designSessionEntity = null;
JavaScriptSerializer jSerializer = null;
string designJsonSession = "null";

designSessionEntity = new DesignEntity();
jSerializer = new JavaScriptSerializer();

designSessionEntity = /* */ // Get Values via some method
designJsonSession = jSerializer.Serialize(designSessionEntity); 
// Now append designDetails to resultant string json

I want to append designDetails to designJsonSession after serialization is complete. 我想追加designDetailsdesignJsonSession序列完成后。 How can i achieve the same ? 我怎样才能达到同样的目的?

First, add one more field to your DesignEntity class. 首先,向您的DesignEntity类添加一个字段。 Whose type is the contract class that designDetails json is serialized from. 谁的类型是designDetails json序列化的合同类。

public class DesignEntity
{
    public string DesignName { get; set; }
    public string DesignID { get; set; }
    /// new field
    public DesignDetailsObject DesignDetails { get; set; }
    [ScriptIgnore]
    public string designDetails { get; set; }
}

And then, get your object as usual, deserialize the json first into that new field, then serialize it. 然后,照常获取对象,首先将json反序列化到该新字段中,然后对其进行序列化。

var designSessionEntity = new DesignEntity();
var jSerializer = new JavaScriptSerializer();

designSessionEntity = /// Get Values via some method

/// assign the design details with deserialized object
designSessionEntity.DesignDetails = jSerializer.Deserialize<DesignDetailsObject>(designSessionEntity.designDetails);

/// serialize it again
var designJsonSession = jSerializer.Serialize(designSessionEntity); 

Voila! 瞧!

Update 更新

If you don't have access to the class that designDetails json is serialized from, then you have two options. 如果您没有访问designDetails json进行序列化的类,则有两个选择。

  1. Try to deduce the class from the json structure and create the DesignDetailsObject yourself (Recommended). 尝试从json结构中推断出该类, DesignDetailsObject自己创建DesignDetailsObject (推荐)。
  2. Append the json string manually (I'll try to show you how if you need) - This method must be applied if the class is really heavy, and won't worth the work. 手动附加json字符串(我将尝试向您展示是否需要)-如果该类确实很繁琐,则不值得使用此方法。

Code Snippet For option 2: 代码段对于选项2:

public string JsonAppender ( string targetJson, List<string> fields, object value )
{
    var insertIndex = 0;
    foreach ( var field in fields )
    {
        var _fieldDescriptor = field + "\":";
        insertIndex += targetJson.Substring(insertIndex).IndexOf(_fieldDescriptor) + _fieldDescriptor.Length;
    }

    var lengthOfDefaultVal = targetJson.Substring(insertIndex).IndexOf("\"") - 1;

    return targetJson.Substring(0, insertIndex) + "\"" + value + "\"" + targetJson.Substring(insertIndex + lengthOfDefaultVal);
}

Usage: 用法:

var fieldAppendedJson = JsonAppender(json, new List<string> { "designDetails " }, designSessionEntity.designDetails);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM