Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从我发送的post请求读取json响应_C#_Json_Windows Phone 8 - Fatal编程技术网

C# 从我发送的post请求读取json响应

C# 从我发送的post请求读取json响应,c#,json,windows-phone-8,C#,Json,Windows Phone 8,我创建了一个post请求类,我可以重用该类向外部api发出post请求,并返回它们发送给我的对象(JSON): 我现在不确定如何获得从API发送给我的回复的主体。它返回一个json格式,我需要能够将它映射到一个漂亮的C#类,并使用它在手机上显示内容 我找不到不使用JSON.NET的示例(它没有适用于windows phone 8的程序集) 这是安装HTTPClient类时出现的错误: Attempting to resolve dependency 'Microsoft.Bcl (≥ 1.1.

我创建了一个post请求类,我可以重用该类向外部api发出post请求,并返回它们发送给我的对象(JSON):

我现在不确定如何获得从API发送给我的回复的主体。它返回一个json格式,我需要能够将它映射到一个漂亮的C#类,并使用它在手机上显示内容

我找不到不使用JSON.NET的示例(它没有适用于windows phone 8的程序集)


这是安装HTTPClient类时出现的错误:

Attempting to resolve dependency 'Microsoft.Bcl (≥ 1.1.3)'.
Attempting to resolve dependency 'Microsoft.Bcl.Build (≥ 1.0.4)'.
Successfully installed 'Microsoft.Bcl.Build 1.0.10'.
Successfully installed 'Microsoft.Bcl 1.1.3'.
Successfully installed 'Microsoft.Net.Http 2.2.13'.
Successfully added 'Microsoft.Bcl.Build 1.0.10' to UnofficialPodio.
Executing script file ***\packages\Microsoft.Bcl.Build.1.0.10\tools\Install.ps1'.
This reference cannot be removed from the project because it is always referenced by the compiler.
This reference cannot be removed from the project because it is always referenced by the compiler.
This reference cannot be removed from the project because it is always referenced by the compiler.
This reference cannot be removed from the project because it is always referenced by the compiler.
Executing script file ***\packages\Microsoft.Bcl.Build.1.0.10\tools\Uninstall.ps1'.
Successfully uninstalled 'Microsoft.Bcl 1.1.3'.
Successfully uninstalled 'Microsoft.Bcl.Build 1.0.10'.
Install failed. Rolling back...
Failed to add reference to 'System.IO'.


JSON.Net确实支持WindowsPhone8,根据答案,它是一个可移植的类库


只需尝试通过nuget…

添加包,以获取在HttpWebResponse对象上调用GetResponseStream()并从流中读取所需的响应数据。大概是这样的:

using (Stream s = response.GetResponseStream())
{
    using (TextReader textReader = new StreamReader(s, true))
    {
        jsonString = textReader.ReadToEnd();
    }
}
    [DataContract]
    public class ApiData
    {
        [DataMember(Name = "name")]  <--this name must be the exact name of the json key
        public string Name { get; set; }

        [DataMember(Name = "description")]
        public string Description { get; set; }
    }
要从json字符串中获取数据,您需要创建一个数据协定类来描述json数据,如下所示:

using (Stream s = response.GetResponseStream())
{
    using (TextReader textReader = new StreamReader(s, true))
    {
        jsonString = textReader.ReadToEnd();
    }
}
    [DataContract]
    public class ApiData
    {
        [DataMember(Name = "name")]  <--this name must be the exact name of the json key
        public string Name { get; set; }

        [DataMember(Name = "description")]
        public string Description { get; set; }
    }
WebRequest可以正常工作,但我建议为HttpClient类安装NuGet包。它使生活简单得多。例如,您可以在几行代码中生成上述请求代码:

        HttpClient httpClient = new HttpClient();
        HttpRequestMessage msg = new HttpRequestMessage(new HttpMethod("POST"), escapedUrl);
        HttpResponseMessage response = await httpClient.SendAsync(msg);
在回答下面的问题时,以下是我使用的通用json转换器代码:

public static class JsonHelper
{
    public static T Deserialize<T>(string json)
    {
        using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            T obj = (T)serializer.ReadObject(stream);
            return obj;
        }
    }

    public static string Serialize(object objectToSerialize)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(objectToSerialize.GetType());
            serializer.WriteObject(ms, objectToSerialize);
            ms.Position = 0;
            using (StreamReader sr = new StreamReader(ms))
            {
                return sr.ReadToEnd();
            }
        }
    }
}
公共静态类JsonHelper
{
公共静态T反序列化(字符串json)
{
使用(MemoryStream stream=new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
DataContractJsonSerializer serializer=新的DataContractJsonSerializer(typeof(T));
T obj=(T)serializer.ReadObject(流);
返回obj;
}
}
公共静态字符串序列化(对象对象对象序列化)
{
使用(MemoryStream ms=new MemoryStream())
{
DataContractJsonSerializer serializer=新的DataContractJsonSerializer(objectToSerialize.GetType());
serializer.WriteObject(ms,objectToSerialize);
ms.Position=0;
使用(StreamReader sr=新StreamReader(ms))
{
返回sr.ReadToEnd();
}
}
}
}

实际上,我无法让上面的HttpClient代码正常工作。。。我回家后会看一看,看看我是否能提醒自己为什么:)我添加了使用nuget安装时出现的错误(适用于windows phone visual studio 2012)是的,它工作得非常好。。。我只是让它工作起来:D。。。谢谢你的回答,伙计。你是个职业选手!为什么我的datacontract类在ReadObject行之后可能是空的?请看我调用的json api返回的(最新编辑)代码,很高兴它能正常工作。要获取“ref”对象,请创建另一个datacontract类,并将“ref”属性的类型设置为新的类类型。我发现,它出现了一个安装错误,即没有为windows phone编写程序集。我回家后会查看你的链接,谢谢。
        using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ApiData));
            ApiData obj = (ApiData)serializer.ReadObject(stream);
            return obj;
        }
        HttpClient httpClient = new HttpClient();
        HttpRequestMessage msg = new HttpRequestMessage(new HttpMethod("POST"), escapedUrl);
        HttpResponseMessage response = await httpClient.SendAsync(msg);
public static class JsonHelper
{
    public static T Deserialize<T>(string json)
    {
        using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            T obj = (T)serializer.ReadObject(stream);
            return obj;
        }
    }

    public static string Serialize(object objectToSerialize)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(objectToSerialize.GetType());
            serializer.WriteObject(ms, objectToSerialize);
            ms.Position = 0;
            using (StreamReader sr = new StreamReader(ms))
            {
                return sr.ReadToEnd();
            }
        }
    }
}