-1

I am using JSON.NET to parse some JSON returned by my server. I feel kind of dumb, because I feel like I'm trying to do something simple. Yet, the code doesn't work. Currently, I'm getting some JSON bck from the server that looks like this:

[
  { id:'1', description:'Some kind of description' },
  { id:'2', description:'Another description' },
  { id:'3', description:'Here is another' }
]

Currently, I'm trying to take this JSON in C# and convert it to a Dictionary<string, string>. My (failing) attempt looks like this:

private Dictionary<string, string> ParseResults(string json)
{
  Dictionary<string, string> dictionary = new Dictionary<string, string>();
  string descriptionPropertyName = "description";
  string descriptionPropertyValue = null;
  bool isDescriptionProperty = false;

  string idPropertyName = "id";
  string idPropertyValue = null;
  bool isIdProperty = false;

  JsonTextReader jsonReader = new JsonTextReader(new StringReader(json));
  while (jsonReader.Read())
  {
    if (jsonReader.TokenType == JsonToken.PropertyName)
    {
      if (descriptionPropertyName.Equals(jsonReader.Value))
      {
        isDescriptionProperty = true;
      }
      else if (idPropertyName.Equals(jsonReader.Value))
      {
        isIdProperty = true;
      }
    }
    else
    {
      if ((isDescriptionProperty == true) && (descriptionPropertyValue == null))
      {
        if (jsonReader.TokenType == JsonToken.String)
        {
          if (jsonReader.Value != null)
          {
            descriptionPropertyValue = jsonReader.Value.ToString();
          }
        }
      }
      else if ((isIdProperty == true) && (idPropertyValue == null))
      {
        if (jsonReader.TokenType == JsonToken.String)
        {
          if (jsonReader.Value != null)
          {
            idPropertyValue = jsonReader.Value.ToString();
          }
        }
      }
    }

    if ((isDescriptionProperty == true) && (isIdProperty == true) && (descriptionPropertyValue != null) && (idPropertyValue != null))
    {
      dictionary.Add(idPropertyValue, descriptionPropertyValue);
    }
  }

  return dictionary;
}

That's a lot of code for something that seems so simple. I feel I have to do something wrong. In fact, its not working, so something has to be wrong. I just can't determine what.

Thank you for any help you may be able to provide. Please remember, this is based on the .NET 2.0 framework.

xam developer
  • 1,923
  • 5
  • 27
  • 39

1 Answers1

0

You don't have to parse the json string manually. You can deserialize it in a strongly typed collection using JSON.NET

class MyClass
{
    public string id { get; set; }
    public string description { get; set; }
}

then

     string json = @"[
                      { id:'1', description:'Some kind of description' },
                      { id:'2', description:'Another description' },
                      { id:'3', description:'Here is another' }
                    ]";

    List<MyClass> collection = JsonConvert.DeserializeObject<List<MyClass>>(json);

    foreach (var item in collection)
        Console.WriteLine(item.id + " - " + item.description);

Or you can convert it to a dictionary

Dictionary<string, string> dict = collection.ToDictionary(e => e.id, e => e.description);
George Vovos
  • 7,563
  • 2
  • 22
  • 45
  • That's awesome! I had not idea you could do that! I'm curious, is there a way to do a mapping during the deserialization? In other words, if my JSON has description, but if I want MyClass to use the property name Description, is there a way to go from 'description' to 'Description'? – xam developer Jul 27 '14 at 14:13
  • Yes : http://stackoverflow.com/questions/15915503/net-newtonsoft-json-deserialize-map-to-a-different-property-name – George Vovos Jul 27 '14 at 14:47