Click here to Skip to main content
16,016,168 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to convert my json array into key/value pairs in a Dictionary but I keep getting my Keys and Values as null. Exception : System.ArgumentNullException: 'Value cannot be null.
Parameter name: key'

I am trying to get them as
"Key" : Value
"Key" : Value

Here is the Json

[
  {
    "id": 1,
    "name": "David",
    "type": 0
  },
  {
    "id": 12,
    "name": "John",
    "type": 0,
  }
]


What I have tried:

var value = JsonConvert.DeserializeObject<List<KeyValuePair<string, object>>>(jsonString).ToDictionary(x => x.Key, y => y.Value);
Posted
Updated 9-Jul-20 20:18pm
Comments
George Swan 10-Jul-20 1:33am    
Can you please give an example of the key and the value that you want to get from your Json string? Do you want the 'id' as the key and the 'name' and 'type' as the value?
PIEBALDconsult 10-Jul-20 9:26am    
Do you have an extraneous COMMA? "type": 0,
[no name] 14-Jul-20 13:22pm    
Post the example of the output you expect. If you want a Dictionary then just deserialize it to it:
var res = JsonConvert.DeserializeObject<list<dictionary<string,object>>>(json);

You do have a dictionary but actually two of them since there are two objects in your collections, you can't just combine them since keys would be duplicated. You need to specify a key like e.g. id and then the value will be e.g an anonymous object with name and type. Depends what you real scenario is.

Assuming you want id as the key and Person as the value, you can do something like this.


C#
 public class Person
  {
      public int id { get; set; }
      public string name { get; set; }
      public int type { get; set; }
  }

string jsonString = @"[
 {
  'id': 1,
  'name': 'David',
  'type': 0
 },
 {
  'id': 12,
  'name': 'John',
  'type': 0
 }
]";

 var dictionary = JsonConvert.DeserializeObject<IEnumerable<Person>>(jsonString).
                  Select(p => (Id: p.id, Record: p)).
                  ToDictionary(t => t.Id, t => t.Record);
 
Share this answer
 
It seems that no value is returned for a key, do a check if a value is empty, something like this might help -

C#
static void Main(String[] args) 
    {
        string number = Console.ReadLine();
        int n;
        Int32.TryParse(number, out n);

        var value = new Dictionary();

        for(int i = 0; i < n; i++)
        {
            //Try to use better naming conventions...
            string myKey = Console.ReadLine();
            string myValue = Console.ReadLine();
            value.Add(myKey, myValue);
        }

        foreach (var pair in value)
        {
            string myKey = pair.Key;
            string myValue = pair.Value;
        }


        string x = Console.ReadLine();
        if(x == null || !value.ContainsKey(x))
        {
            Console.WriteLine("Not Found");
        }
        else
        {
            string result = value[x];
            Console.Write(x);
            Console.Write("=");
            Console.Write(result);
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900