3

我正在使用 Visual Studio C# 中的 asmx Web 服务。示例代码

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。

谢谢

4

1 回答 1

0

我认为您只需要解析 JSON,并获取 key="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
于 2013-03-22T16:31:33.670 回答