[英]WCF Deserialize JSON String
I had edited my question : 我已经编辑了我的问题:
How can i deserialize the JSON string shows below : 我如何反序列化JSON字符串,如下所示:
"{\"acctId\": \"Test10001\",\"amount\": 200,\"currency\": \"USD\",\"Code\": \"Test\",\"serialNo\": \"1234566789asdsad0\"}"
Please give suggestion how can I get the data by using this method or any other recommended method. 请提出建议,如何使用此方法或任何其他推荐方法获取数据。
Suggesting you to use StreamWriter
as below. 建议您如下使用
StreamWriter
。 Use this function and pass your string and return a the Stream which will give you the desired JSON Content 使用此函数并传递您的字符串并返回一个Stream,它将为您提供所需的JSON内容
public static Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
you can read Json string like this 您可以像这样读取Json字符串
dynamic stuff = JObject.Parse(res.ToString());
string acctId= stuff.acctId;
But the response string you are parsing should be json formatted. 但是您要解析的响应字符串应为json格式。
I think below code should serve your purpose: 我认为下面的代码应达到您的目的:
public class DeserializedData
{
public string acctId { get; set; }
public string amount { get; set; }
public string currency { get; set; }
public string Code { get; set; }
public string serialNo { get; set; }
}
StreamReader reader = new StreamReader(streamdata);
string res = reader.ReadToEnd();
Use third party dlls like Json.NET or Restsharp: 使用第三方dll(例如Json.NET或Restsharp):
1.) Using Json.Net Json.NET 1.)使用Json.Net Json.NET
var result = JsonConvert.DeserializeObject<DeserializedData>(res);
2.) Using Restsharp Restsharp 2.)使用Restsharp Restsharp
var jsonDeserializer = new RestSharp.Deserializers.JsonDeserializer();
var response = jsonDeserializer.Deserialize<DeserializedData>(res);
Let me know if it doesn't work for you. 让我知道这是否对您不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.