4

I have following json:

{   
    "serverTime": "2013-08-12 02:45:55,558",
    "data": [
        {
            "key1": 1,
            "key2": {},
            "key3": {
                "key4": [
                    ""
                ],
                "key5": "test2"
            },
            "key7": 0
        },
        {
            "key8": 1,
            "key9": {},
            "key10": {
                "key4": [
                    ""
                ],
                "key9": "test2"
            },
            "key11": 0
        }
    ] 

}

I want to get values as key value pair. Something like:

jsonObject[data][0]

should give first item of the data array.

I am using JSONFx.net. But it gives strongly typed objects. I do not want that. Is there any way to parse JSON as key value as I mentioned earlier?

Thanks

Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
Ashwani K
  • 7,880
  • 19
  • 63
  • 102
  • It's because the json you wrote here is not an array but an object. And this object is holding an array(data) in it. The way you'll extract the array out of this json object is up to you. Right now there exist useful methods in answers though. – Tolga Evcimen Aug 12 '13 at 12:52

4 Answers4

6

Try this:

using System;
using System.IO;
using Newtonsoft.Json;

class Program
{
    static void Main(string[] args)
    {
        var json = File.ReadAllText("input.txt");
        var a = new { serverTime = "", data = new object[] { } };
        var c = new JsonSerializer();
        dynamic jsonObject = c.Deserialize(new StringReader(json), a.GetType());
        Console.WriteLine(jsonObject.data[0]);
    }
}
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
3

If you're not averse to using Json.NET, you can do this:

var jsonString = @"
{   
    ""serverTime"": ""2013-08-12 02:45:55,558"",
    ""data"": [
        {
            ""key1"": 1,
            ""key2"": {},
            ""key3"": {
                ""key4"": [
                    """"
                ],
                ""key5"": ""test2""
            },
            ""key7"": 0
        },
        {
            ""key8"": 1,
            ""key9"": {},
            ""key10"": {
                ""key4"": [
                    """"
                ],
                ""key9"": ""test2""
            },
            ""key11"": 0
        }
    ] 
}";

var jsonResult = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(jsonString);
var firstItem = jsonResult["data"][0];

firstItem would be an array of the first item in the data array:

Demo result

Hope this helps.

Sameer Singh
  • 1,358
  • 1
  • 19
  • 47
1

First create classes to parse string

public class Key2
{
}

public class Key3
{
    public List<string> key4 { get; set; }
    public string key5 { get; set; }
}

public class Key9
{
}

public class Key10
{
    public List<string> key4 { get; set; }
    public string key9 { get; set; }
}

public class Datum
{
    public int key1 { get; set; }
    public Key2 key2 { get; set; }
    public Key3 key3 { get; set; }
    public int key7 { get; set; }
    public int? key8 { get; set; }
    public Key9 key9 { get; set; }
    public Key10 key10 { get; set; }
    public int? key11 { get; set; }
}

public class RootObject
{
    public string serverTime { get; set; }
    public List<Datum> data { get; set; }
}

add reference of Newtonsoft.Json.dll

RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonData);

then you can access values .

Nirmal
  • 924
  • 4
  • 9
1

If you want to do this without third party libraries then do:

I would use the following code:

var deserializer = new JavaScriptSerializer();
var someObject = deserializer.DeserializeObject(json);

string serverTime = someObject["serverTime"].ToString();
Dictionary<string, int> data = someObject["data"] as Dictionary<string, int>;

Give it a go.

Edit: You may need to change the last line to:

Dictionary<string, int?> data = someObject["data"] as Dictionary<string, int?>;
Base33
  • 3,167
  • 2
  • 27
  • 31