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);
7
  • 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<Dictionary<string, object>>(json);. Also, there is no need to replace the new lines. Commented Apr 19, 2019 at 6: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. Commented Apr 19, 2019 at 6: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. Commented Apr 19, 2019 at 7:01
  • @ckuri, if you not sure the what the type of json property then you can use JToken Commented Apr 19, 2019 at 7: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. Commented Apr 19, 2019 at 7:07

3 Answers 3

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[]{};
Sign up to request clarification or add additional context in comments.

1 Comment

thank you! this solution is very useful.
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; }
}

Comments

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();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.