Newtonsoft JSon反序列化为Primitive类型

时间:2016-02-09 14:58:52

标签: c# .net json json.net deserialization

在我的C#程序中,我正在查询Web服务并使用JSON获取回复流,如下所示:

{"val":12345.12},{"val":23456.23},{"val":34567.01},...

或每个回复对象可能超过1个值:

{"val1":12345.12,"val2":1},{"val1":23456.23,"val2":3},....

我有以下代码利用Newtonsoft.Json库解析流并对每个解析的对象执行一些操作,一次一个:

public void ParseReply<T>(StreamReader sr, Action<T> action)
{
    using (var reader = new JsonTextReader(sr))
    {
        var ser = new JsonSerializer();

        while (reader.Read())
        {
            if (reader.TokenType == JsonToken.EndArray)
                break;
            action(ser.Deserialize<T>(reader));
        }
    }
}

因此,在第二个结果的情况下,我有以下代码:

public class MyObject
{
    public double val1;
    public double val2;
}

然后:

myJson.ParseReply<MyObject>(sr, obj => ...);

完美无缺。

但是,在第一个回复的情况下(每个对象1个值),如果我尝试以下列方式使用我的方法:

myJson.ParseReply<double>(sr, dbl => ...);

我收到错误说:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Double' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

我真的很遗憾/好奇我如何更新我的代码以便能够正确地解析这两个代码而且我对此错误消息有点迷失。任何帮助真的很值得赞赏!!

1 个答案:

答案 0 :(得分:1)

如果您更改静态方法以返回IEnumerable<T>而不是Action<T>,那么您将能够以非常简洁的方式将EnumerableLINQ to JSON方法链接在一起和自然的方式。因此,如果你有:

public static class JsonExtensions
{
    public static IEnumerable<T> ParseArray<T>(this TextReader textReader)
    {
        using (var reader = new JsonTextReader(textReader))
        {
            bool inArray = false;
            var ser = JsonSerializer.CreateDefault();
            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.Comment)
                    continue;
                if (reader.TokenType == JsonToken.StartArray && !inArray)
                {
                    inArray = true;
                    continue;
                }
                if (reader.TokenType == JsonToken.EndArray)
                    break;
                yield return ser.Deserialize<T>(reader);
            }
        }
    }

    public static IEnumerable<JToken> DescendantsAndSelf(this JToken node)
    {
        // This method should be present on JToken but is only present on JContainer.
        if (node == null)
            return Enumerable.Empty<JToken>();
        var container = node as JContainer;
        if (container != null)
            return container.DescendantsAndSelf();
        else
            return new[] { node };
    }

    public static bool IsNumeric(this JTokenType type) { return type == JTokenType.Integer || type == JTokenType.Float; }

    public static bool IsNumeric(this JToken token) { return token == null ? false : token.Type.IsNumeric(); }
}

你可以这样做:

        var json = @"[{""val"":12345.12},{""val"":23456.23},{""val"":34567.01}]";

        // Select all properties named "val" and transform their values to doubles.
        foreach (var val in new StringReader(json).ParseArray<JToken>()
            .Select(t => (double)t.SelectToken("val")))
        {
            Debug.WriteLine(val);
        }

        // Select all primitive numeric values
        foreach (var val in new StringReader(json).ParseArray<JToken>()
            .SelectMany(t => t.DescendantsAndSelf())
            .Where(t => t.IsNumeric())
            .Select(t => (double)t))
        {
            Debug.WriteLine(val);
        }