Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse JSON with ServiceStack

Tags:

json

c#

How to parse this json string? I tried to put in in Dictionary

var dictionary = text.FromJson<Dictionary<string, string>>();

but the array is not parsed.

{"v":[[9,-1],[9,-44,1]]}
like image 823
igla Avatar asked Dec 01 '25 09:12

igla


2 Answers

try this class

public class Root
{
     public  List<List<int>> v;
}

var result = text.FromJson<Root>();

EDIT

Since your json string has changed, I prepared a sample using Json.Net

string json = @"{ v: [ [ 9, 16929, 1, 856, 128, '123', 'hello', {'type': 'photo', 'attach1': '123_456'} ] ] } ";
var obj = (JObject)JsonConvert.DeserializeObject(json);

foreach (var arr in obj["v"])
{
    foreach(var item in arr)
    {
        if (item is JValue)
        {
            Console.WriteLine(item);
        }
        else
        {
            Console.WriteLine(">>> " + item["type"]);
        }
    }
}
like image 101
L.B Avatar answered Dec 04 '25 01:12

L.B


You use the wrong structure. You value is a multi demensional array and no string. Try the type Dictionary<String, List<List<int>>>

var dictionary = text.FromJson< Dictionary<String, List<List<int>>>>();
like image 25
rekire Avatar answered Dec 03 '25 23:12

rekire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!