Вопрос

Lets assume I have the following JSON Object:

{
    "Key": "\"QTuY+0m31w2QiZGl4h+W8w==\"",
    "Value":
    [
        "addgroup",
        "viewgroup",
        "editgroup"
    ]
}

How can I deserialize the Value part in a C# string[]?

The problem is, string[] is not the only type I have in the Value part, it can be everything, so I need to use dynamic.

I am using JSON.net and when I use the JsonConvert.DeserializeObject method I get a runtime binder exception.

This is the method I use to deserialize:

public async Task<Tuple<string, dynamic>> ReadCachingEntryAsync(string uri) {
    var data = tools.ReadEntry(Path.Combine(cacheFolder, uri.Replace("/", "")));
    var res = new Tuple<string, dynamic>(string.Empty, null);
    if (data != null) {
        var dat = JsonConvert.DeserializeObject<KeyValuePair<string, dynamic>>(UTF8Encoding.UTF8.GetString(data));
        res = new Tuple<string, dynamic>(dat.Key, dat.Value);
    }
    return res;
}

The tools.ReadEntry(data) returns a byte[] containing the data, from the binary formatted file.

Here is the class KeyValuePair<K, V>

[Serializable]
internal struct KeyValuePair<K, V> {
    public K Key { get; set; }
    public V Value { get; set; }
}
Это было полезно?

Решение 2

You can check if it's a JArray and implement special handling. E.g.

byte[] data = Encoding.UTF8.GetBytes(@"{""Key"": ""Something"", ""Value"": [""test"", ""test 2"", ""test 3""]}");
var dat = JsonConvert.DeserializeObject<KeyValuePair<string, dynamic>>(UTF8Encoding.UTF8.GetString(data));
var value = dat.Value;
if (value is JArray)
{
    var arrayType = value[0].Value.GetType().MakeArrayType();
    value = value.ToObject(arrayType);
}
var res = new Tuple<string, dynamic>(dat.Key, value);

Другие советы

Just call DeserializeObject<T> replacing T with the type you intend to deserialize the input string into and json.NET will take care of the rest.

string[] json = JsonConvert.DeserializeObject<string[]>(inputString);

EDIT:

Ok, so assuming all of your json is in the form of the example posted in EDIT 3 then waht you really want is;

public class KVPair
{
    public string Key { get; set; }
    public string[] Value { get; set; }
}

KVPair pair = JsonConvert.DeserializeObject<KVPair>(UTF8Encoding.UTF8.GetString(data));

Now this is fairly static but I'm not sure you actually need dynamic. If you do then you'll want the Value property to be of type object or dynamic. However, if you go that route then you will unfortunately have to deal with this unknown type. I'm really not knowledgeable on the dynamic type so I can't comment on how to deal with it later on but if say for example, the array could contain, ints, floats or strings and you made Value of type object you would have inspect the array later on, determine the underlying type and cast each of the items to that type before you could do anything useful with them. Hope that helps. If you're lucky you'll just be able to use the code I posted above as it's far simpler and is actually type safe.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top