0

I am new to .net and I am trying to convert JSON string to object. I have written following code but it gives me syntax errors:

JavaScriptSerializer JSS = new JavaScriptSerializer();
T obj = JSS.Deserialize<T>(String);

It doesn't recognize T in the code. Please help.

I dont want to create any custom class. Can I get JSON from json string which I can use to find values of given keys

3 Answers3

1

You didn't specify T in anywhere.This code should be inside of a generic class or method where T is specified as generic type parameter.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • In python, we can get json(dictionary) from json string. Can I get JSON from json string which I can use to find values of given keys. –  Jun 03 '14 at 10:42
1

Taking your code literally:

JavaScriptSerializer JSS = new JavaScriptSerializer();
T obj = JSS.Deserialize<T>(String);

String is a type, not an object. You need to pass in the variable you want to deserialize:

public class Person
{
   public int Id { get;set; }
   public string Name { get;set; }
}

// Then somewhere else

string json = @"{ ""Id"": 10, ""Name"": ""Jeremy Vines"" }";

JavaScriptSerializer JSS = new JavaScriptSerializer();
Person obj = JSS.Deserialize<Person>(json);

Console.WriteLine("Id: {0}, Name: {1}", obj.Id, obj.Name);
Dominic Zukiewicz
  • 8,258
  • 8
  • 43
  • 61
0

Try replacing T with the object type that you are expecting to get. Or even object, if you don't know.

nl-x
  • 11,762
  • 7
  • 33
  • 61