2

Test with JSON like this:

{\"key1\":\"value1\",\"key2\":\"value3\",\"key3\":[\"value4\"],\"key5\":\"value5\"}\n;

invalid arguments exception: 'Newtonsoft.Json.JsonConvert.DeserializeObject>(string)'

This is my code :

string json = "{\"action\":\"recognition\",\"command\":\"event\",\"eventid\":[\"1108\"],\"from_ip\":\"192.168.0.49\",\"user\":\"safetymaster\"}\n";
json = json.Replace("\n", "");  

var DeserializedJson= JsonConvert.DeserializeObject<dynamic>(json);                                
Dictionary<string, string> jsonDic = JsonConvert.DeserializeObject<Dictionary<string, string>>(DeserializedJson);
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Caglia
  • 19
  • 1
  • 5
  • 2
    `key3`/`eventid` does not have a string value, but an array of string. I don’t see what you are trying to achieve by calling DeserializeObject twice. Thus, it should just be `var jsonDic = JsonConvert.DeserializeObject>(json);`. Also, there is no need to replace the new lines. – ckuri Apr 19 '19 at 06:39
  • @ckuri, if you use `object` then you may need to cast it to user type and its increases complexity, you can either use `JToken` instead. – er-sho Apr 19 '19 at 06:45
  • @er-sho You are correct. I assumed the eventid list would be deserialized into a list of objects and not a JArray, which makes accessing the list entries awkward (i.e. different from accessing the simple properties where you just need to cast to a string). Of course, the best solution would be if he deserialized it into a proper class with properly specified properties and types for better convenience and type-safety. – ckuri Apr 19 '19 at 07:01
  • @ckuri, if you not sure the what the type of json property then you can use `JToken` – er-sho Apr 19 '19 at 07:02
  • 1
    When Json change and evolve in a convulted manner, JToken is a strong tool. We target only the properties we need. F.e "All the sub properties name Foo no matter where they are under the FooBar node." is simple. Json served by not strongly typed language with shapeshifting property (Php array, I'm looking at you) Often need the use Custom converter or JToken. But when the Json is know. Class have great benefit. They can both achieve the same thing. – xdtTransform Apr 19 '19 at 07:07
  • But that's a great chat about Json, But can we find a any Json question with few property and a array/list/collection and close this one as dupe? Before we start to get answer ranging from Json2Csharp to "RootObject definition and JsonConvert.DeserializeObject" – xdtTransform Apr 19 '19 at 07:10
  • @Caglia If my answer was helpful to you, please accept it. – Sachith Wickramaarachchi Apr 19 '19 at 10:40

3 Answers3

1

Try this.

class Program
{
    static void Main(string[] args)
    {
        try {
            string json = "{\"action\":\"recognition\",\"command\":\"event\",\"eventid\":[\"1108\"],\"from_ip\":\"192.168.0.49\",\"user\":\"safetymaster\"}\n";
            json = json.Replace("\n", "");
            RootObject m = JsonConvert.DeserializeObject<RootObject>(json);
            string ipAddress = m.from_ip;
            string eventID = m.eventid[0];

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }

    }
}

Define a class according to the JSON object.

public class RootObject
{
    public string action { get; set; }
    public string command { get; set; }
    public List<string> eventid { get; set; }
    public string from_ip { get; set; }
    public string user { get; set; }
}
Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
1

This can be done with a 2-step process:

  1. Dserialise as a Dictionary< string, object >
  2. Use System.Linq functionality (ToDictionary) to convert the previous result to Dictionary< string, string >

For example:

string json = "{\"action\":\"recognition\",\"command\":\"event\",\"eventid\":[\"1108\"],\"from_ip\":\"192.168.0.49\",\"user\":\"safetymaster\"}\n"; json = json.Replace("\n", "");

Dictionary<string, object> jsonDic = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);

var jsonDic2 = jsonDic.ToDictionary(
    x => x.Key, 
    x => x.Value.ToString()
);

The result of the above in jsonDic2 is a Dictionary with the contents:

enter image description here

Please note: the string value of the "eventid" dictionary entry is formatted as a JSON array, and so you may need to convert its value out as a string array when necessary:

var eventIdList = jsonDic2.ContainsKey("eventid")
    ? Newtonsoft.Json.JsonConvert.DeserializeObject<string[]>(jsonDic2["eventid"])
    : new string[]{};
axanull
  • 146
  • 4
0

You can simply get value:

 string json = "{\"action\":\"recognition\",\"command\":\"event\",\"eventid\":[\"1108\"],\"from_ip\":\"192.168.0.49\",\"user\":\"safetymaster\"}\n";            
 var jsonData = JsonConvert.DeserializeObject<JObject>(json);
 var action = jsonData["action"].ToString();
A.M. Patel
  • 334
  • 2
  • 9