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
  • Duplicate: stackoverflow.com/questions/5979434/… Commented Jun 3, 2014 at 10:36
  • Don't you need a class to deserialize, rather than a type e.g. String? Commented Jun 3, 2014 at 10:37
  • @gliese581g: 'Can I get JSON from json string which I can use to find values of given keys' - You mean like a Dictionary<string, object>? No, you can't. Commented Jun 3, 2014 at 10:49

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.