0

I tried to read all the Request and response using Fiddler but in some cases I am getting the request data as JSON, and I want to parse that JSON into a key value pair.

I have tried like this

string requestPost = Utilities.UrlDecode(oSession.GetRequestBodyAsString());
Dictionary<string, string> jsonRequest = new Dictionary<string, string>();
jsonRequest = JsonConvert.DeserializeObject<Dictionary<string, string>>(requestPost);
dataGridViewFiddler.DataSource = jsonRequest;

But this didn't work for me.

this is my Json format

{"entries":[{"http_response_code":200,"network_changed":false,"protocol":"QUIC","request_age_ms":112425,"request_elapsed_ms":27,"sample_rate":0.05,"server_ip":"216.58.197.78:443","status":"ok","url":"https://www.google-analytics.com/","was_proxied":false}],"reporter":"chrome"}

Now if i use dynamic to parse this json its working but

enter image description here

Ravi Kanth
  • 1,182
  • 13
  • 38
  • Please can you tell us what `requestPost` looks like? – Luke Apr 21 '17 at 08:22
  • @Luke requestpost may be any json.. – Ravi Kanth Apr 21 '17 at 08:45
  • If your JSON really can be anything at all, then "[How do I use JSON.NET to deserialize into nested/recursive Dictionary and List?](http://stackoverflow.com/q/5546142/10263)" might help you. But be aware that not every JSON can be parsed into key-value pairs. If you are starting with an array in JSON, then you will end up with a list when you parse it. – Brian Rogers Apr 25 '17 at 07:12

2 Answers2

3

You indicate that you can receive any JSON. However, this will result in severe challenges, because you still have to parse your data to use it in some structured way. Therefore you should make sure you always get a similar input. I will give you to options that are best suited.

Option 1: De-serializing JSON to a Dictionary only works if your data follows the key/value layout. So the layout should always be like this:

{
    "key1": "value1",
    "key2": "value2"
}

Of course, your value can also be a list. However, the basic layout is still the same. For example:

{
    "key1": ["value1", "value2"],
    "key2": ["value3", "value4"]
}

This you can just deserialize with your code:

var dict = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(requestPost);

Option 2: if your JSON data is structured with custom data names, you will have to create corresponding models. A good tool that can put you on the right track for this is json2csharp. Let me give an example. If this is your JSON:

[{
    "Key": "MyKey",
    "Values": [{
        "First": "RandomValue",
        "Second": "RandomValue"
    }, {
        "First": "RandomValue",
        "Second": "RandomValue"
    }]
},
{
    "Key": "MyKey",
    "Values": [{
        "First": "RandomValue",
        "Second": "RandomValue"
    }, {
        "First": "RandomValue",
        "Second": "RandomValue"
    }]
}]

Then your corresponding models should be:

public class Value
{
    public string First { get; set; }
    public string Second { get; set; }
}

public class RootObject
{
    public string Key { get; set; }
    public List<Value> Values { get; set; }
}

And you can deserialize it like this:

var values = JsonConvert.DeserializeObject<List<RootObject>>(json);

Concluding, you can either make sure your JSON data is structured as a Dictionary with key/value pairs. In that case you can directly deserialize to a Dictionary. Otherwise you will have to create custom models to fit your JSON data. Use whatever works best for you! Please note that it is possible to just deserialize random data, but it will make it very hard to make sure your code can handle the data! See Luke's answer on how to do this.

Community
  • 1
  • 1
Jurjen
  • 1,376
  • 12
  • 19
1

If you want to be able to deserialize any JSON, then the type that you need to deserialize to is simply dynamic, not Dictionary<string, dynamic> because this would mean that you would have to have a string key in your JSON...

Therefore, to deserialize any JSON object, as requested, you need to use the code:

var json = JsonConvert.DeserializeObject(requestPost);

Bearing in mind that JSON can be as simple as:

["bob", "fred"]

Which would not match your model, as there is no key (string in your Dictionary).

Luke
  • 22,826
  • 31
  • 110
  • 193