【问题标题】:How do I incrementally serialize and deserialize JSON with ServiceStack?如何使用 ServiceStack 增量序列化和反序列化 JSON?
【发布时间】:2013-03-27 09:11:28
【问题描述】:

我拥有的是这样的:

string json = @"{'number': 3, 'object' : { 't' : 3, 'whatever' : 'hi', 'str': 'test'}";

我如何读取字段,直到我位于“对象”,然后将整个“对象”序列化为 .NET 类型,然后继续解析?

【问题讨论】:

    标签: c# json .net-3.5 servicestack


    【解决方案1】:

    定义你的类型:

    public class Object
    {
        public int t { get; set; }
        public string whatever { get; set; }
        public string str { get; set; }
    }
    
    public class RootObject
    {
        public int number { get; set; }
        public Object object { get; set; }
    }
    

    然后反序列化它:

    string json = @"{'number': 3, 'object' : { 't' : 3, 'whatever' : 'hi', 'str': 'test'}";
    var deserialized = JsonConvert.DeserializeObject<RootObject>(json);
    //do what you want
    

    更新

    你没有说它是动态的,对于这样的解析有很多解决方案。

    检查以下内容:

    Using JSON.NET for dynamic JSON parsing

    Using C# 4.0 and dynamic to parse JSON

    Deserialize JSON into C# dynamic object?

    Parse JSON block with dynamic variables

    Turning JSON into a ExpandoObject

    处理动态类型:使用dynamic,处理动态数据如XMLJSON使用ExpandoObject

    更新 2

    Using Anonymous types to deserialize JSON data

    更新 3

    这对你有用吗:

     string json = "{\"number\": 3, \"object\" : { \"t\" : 3, \"whatever\" : \"hi\", \"str\": \"test\"}}";
                var deserialized = SimpleJson.DeserializeObject<IDictionary<string, object>>(json);
    
                var yourObject = deserialized["object"] as IDictionary<string, object>;            
                if (yourObject != null)
                {
                    var tValue = yourObject.GetValue("t");
                    var whateverValue = yourObject.GetValue("whatever");
                    var strValue = yourObject.GetValue("str");
                } 
    
     public static object GetValue(this IDictionary<string,object> yourObject, string propertyName)
            {
                return yourObject.FirstOrDefault(p => p.Key == propertyName).Value;
            }
    

    最终结果:

    或者改成如下

    if (yourObject != null)
                {
                    foreach (string key in yourObject.Keys)
                    {
                        var myValue = yourObject.GetValue(key);
                    }
                } 
    

    更新 4 - 服务堆栈

    string json = "{\"number\": 3, \"object\" : { \"t\" : 3, \"whatever\" : \"hi\", \"str\": \"test\"}}";
                var deserialized = JsonObject.Parse(json);
    
                var yourObject = deserialized.Get<IDictionary<string, object>>("object");
    
                if (yourObject != null)
                {
                    foreach (string key in yourObject.Keys)
                    {
                        var myValue = yourObject.GetValue(key);
                    }
                }
    

    结果:

    【讨论】:

    • 我不知道类型,json 字符串可以是任何东西——这就是为什么我需要逐步执行它的全部意义。我想偷看key,然后得到对应的值。几乎任何 C json 库都可以做到这一点。
    • plurby,我的要求是它与 .NET 3.5 兼容。动态关键字不是。
    • @Blub 您应该在问题中说明此要求。请参阅我的更新 2。
    • 我做了,它是一个标签。而且您的链接实际上仍然对我没有帮助,因为匿名类型仍然是具有所有字段的 c# 类型。在序列化 json 字符串之前我不知道这些字段。
    【解决方案2】:

    ServiceStack's Dynamic JSON Parsing

    var myPoco = JsonObject.Parse(json)
        .GetUnescpaed("object")
        .FromJson<TMyPoco>();
    

    【讨论】:

    • 在不知道我的参数叫什么的时候没有帮助,而且我仍然不知道如何序列化。 JsonObject.WriteValue() 是一个完全无法使用的 API,甚至不知道它应该做什么。
    • @Blub 遍历一遍,JsonObject 继承Dictionary&lt;string,string&gt;
    【解决方案3】:

    这适用于反序列化,我会在序列化后更新。

    foreach(KeyValuePair<String,String> entry in JsonObject.Parse(json))
    {
    
    }
    

    编辑:看起来这只适用于 json 对象。我仍然不知道如何迭代 JsonArrayObjects

    【讨论】:

      猜你喜欢
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 2012-09-05
      • 2020-10-23
      • 1970-01-01
      相关资源
      最近更新 更多