我有这段代码:
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代码获取json字符串并写入jsonString。 jsonString结果返回
{"id":"54","name":"reza"}
如何将jsonString转换为JsonObject并解析它?
答案 0 :(得分:1)
您可以将NewtonSoft json库用于c#并使用以下代码
创建一个类来保存结果
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;
答案 1 :(得分:0)
解决方案1 使用Newtonsoft.Json(从这里获取包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;
}
解决方案2 请按照以下帖子
http://www.codeproject.com/Tips/79435/Deserialize-JSON-with-C