2013-03-22 56 views
2

我正在从Visual Studio c#中使用asmx web服务。示例代码如何从c#中删除d:from json response?

WebRequest request = WebRequest.Create(WholeURL); 
request.ContentType = "application/json; charset=utf-8"; 
request.Method = "POST"; 
request.ContentLength = postData.Length; 


using (StreamWriter requester = new StreamWriter(request.GetRequestStream())) 
{ 
requester.Write(postData); 
requester.Close(); 
} 

//Get the response 
WebResponse response = request.GetResponse(); 
// Get the stream containing content returned by the server. 
Stream dataStream = response.GetResponseStream(); 
// Open the stream using a StreamReader for easy access. 
StreamReader reader = new StreamReader(dataStream); 
// Read the content.ok 
string responseFromServer = reader.ReadToEnd(); 

如何从JSON响应删除d

我的JSON响应为:{ “d”:-1} 我只需要-1。

感谢

+0

你是不是想改变服务器,因此只给你'-1'或者是你想改变客户端,以便它知道如何正确地解析响应?如果您尝试更改服务器,那么让我们看看服务器上的代码是什么样的 - 正常的.asmx返回的是SOAP消息,而不是JSON,因此我们需要知道您当前如何发送响应。 – 2013-03-22 16:36:59

回答

0

我想你只需要解析JSON,并抢在关键=“d”的值。

使用JSON.NET

string json = @"{""key1"":""value1"",""key2"":""value2""}"; 
Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json); 

Console.WriteLine(values.Count); // 2 
Console.WriteLine(values["key1"]); // value1