I have this code:
public void ProcessRequest(HttpContext context)
{
var jsonSerializer = new JavaScriptSerializer();
var jsonString = String.Empty;
context.Request.InputStream.Position = 0;
using (var inputStream = new StreamReader(context.Request.InputStream))
{
jsonString = inputStream.ReadToEnd();
}
}
Up code get json string and write in jsonString. jsonString result return
{"id":"54","name":"reza"}
How can i convert jsonString to JsonObject and parse it?
John Saunders
162k26 gold badges252 silver badges403 bronze badges
asked Jul 6, 2014 at 5:19
-
I have edited your title. Please see, "Should questions include "tags" in their titles?", where the consensus is "no, they should not".John Saunders– John Saunders2014年07月06日 05:21:25 +00:00Commented Jul 6, 2014 at 5:21
2 Answers 2
You can use NewtonSoft json library for c# and use below code
Create a class to hold the result
Class Person
{
public int id {get;set;}
public string name {get;set;}
}
var person = JsonConvert.DeserializeObject<Person>(jsonString);
if you dont want to create class use JObject
dynamic newObj = JObject.Parse(jsonString);
string id= newObj.id ;
string name= newObj.name;
answered Jul 6, 2014 at 5:51
Sign up to request clarification or add additional context in comments.
1 Comment
Neville Nazerane
This works create with .net standard 1.0, and is probably the only serialization that works with this framework.
Solution 1 Use Newtonsoft.Json (Get package from here https://www.nuget.org/packages/newtonsoft.json/)
using (var inputStream = new StreamReader(context.Request.InputStream))
{
var jsonString = inputStream.ReadToEnd();
var data = JsonConvert.DeserializeObject<Dictionary<string,string>>(jsonString);
return data;
}
Solution 2 Please follow the following Post
http://www.codeproject.com/Tips/79435/Deserialize-JSON-with-C
answered Jul 6, 2014 at 5:50
Comments
lang-cs