Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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# 获取要在文本框中显示的JSON响应消息的一部分时出错_C#_Json_Uwp - Fatal编程技术网

C# 获取要在文本框中显示的JSON响应消息的一部分时出错

C# 获取要在文本框中显示的JSON响应消息的一部分时出错,c#,json,uwp,C#,Json,Uwp,我正在UWP应用程序中解析JSON数据,这是我目前的工作代码: public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } private async void Button_Click(object sender, RoutedEventArgs e) { UsersObject My

我正在UWP应用程序中解析JSON数据,这是我目前的工作代码:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {


        UsersObject MyGetRequest = await GetRequest();



        ResultTextBlock.Text = MyGetRequest.ToString() + " - ";

    }


  public static async Task<UsersObject> GetRequest()
    {
        Uri geturi = new Uri("url"); //replace your url
        System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
        System.Net.Http.HttpResponseMessage responseGet = await client.GetAsync(geturi);
        string response = await responseGet.Content.ReadAsStringAsync();
        //return response;

        var json = await responseGet.Content.ReadAsStringAsync();
        var result = JsonConvert.DeserializeObject<UsersObject>(json);
        return result;
    }

]

问题是服务器返回一个
列表。您正试图将其转换为一个
UsersObject
,它只包含一个
列表
,而不是一个
列表
本身

您需要执行以下操作:

public static async Task<List<UserObject>> GetRequest()
{
    Uri geturi = new Uri("url"); //replace your url
    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
    System.Net.Http.HttpResponseMessage responseGet = await client.GetAsync(geturi);
    string response = await responseGet.Content.ReadAsStringAsync();
    //return response;

    var json = await responseGet.Content.ReadAsStringAsync();
    var result = JsonConvert.DeserializeObject<List<UserObject>>(json);
    return result;
}


private async void Button_Click(object sender, RoutedEventArgs e)
{
    UsersObject myGetRequest = new UsersObject();
    myGetRequest.users = await GetRequest();

    //to iterate all users you got do this:
    foreach(var item in myGetRequest.users)
    {
        //do something
    }
}
公共静态异步任务GetRequest()
{
Uri geturi=newURI(“url”);//替换您的url
System.Net.Http.HttpClient client=新系统.Net.Http.HttpClient();
System.Net.Http.httpresponsemessageresponseget=wait client.GetAsync(geturi);
string response=await responseGet.Content.ReadAsStringAsync();
//返回响应;
var json=await responseGet.Content.ReadAsStringAsync();
var result=JsonConvert.DeserializeObject(json);
返回结果;
}
专用异步无效按钮\u单击(对象发送方,路由目标)
{
UsersObject myGetRequest=newusersobject();
myGetRequest.users=等待GetRequest();
//要迭代所有用户,请执行以下操作:
foreach(myGetRequest.users中的var项)
{
//做点什么
}
}

您能在这里调试并粘贴“json”变量的内容吗?@Declan问题在于反序列化服务器的响应。它在您等待对象时返回数组。如果没有一个示例响应,我能给出的最佳猜测是您应该执行
JsonConvert.DeserializeObject(json)这给了我2个错误:严重性代码描述项目文件行抑制状态无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”,并且:名称“Console”在当前上下文中不存在抱歉出现第一个错误。我将在我的示例中解决这个问题。我不知道你使用的应用程序是否是控制台应用程序。只需删除控制台。writline。。。它只是向您展示了如何在DeclanS上通过所有用户进行迭代。只要把整个东西再粘贴一遍,看看它是否编译:)我明白了“mscorlib.ni.dll中发生'Newtonsoft.Json.JsonReaderException'类型的异常,但未在用户代码中处理其他信息:解析值时遇到意外字符:Json字符串仍然完全相同?您确定粘贴了完整的json吗?变量中的所有内容?
[
    {
        "username": "user1",
        "password": "10",
        "Id": 1,
        "userscol": null,
        "__href": "/db/GroupDBTest/users/Id/1.json"
    },
{
        "username": "user2",
        "password": "100",
        "Id": 2,
        "userscol": null,
        "__href": "/db/GroupDBTest/users/Id/2.json"
}
public static async Task<List<UserObject>> GetRequest()
{
    Uri geturi = new Uri("url"); //replace your url
    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
    System.Net.Http.HttpResponseMessage responseGet = await client.GetAsync(geturi);
    string response = await responseGet.Content.ReadAsStringAsync();
    //return response;

    var json = await responseGet.Content.ReadAsStringAsync();
    var result = JsonConvert.DeserializeObject<List<UserObject>>(json);
    return result;
}


private async void Button_Click(object sender, RoutedEventArgs e)
{
    UsersObject myGetRequest = new UsersObject();
    myGetRequest.users = await GetRequest();

    //to iterate all users you got do this:
    foreach(var item in myGetRequest.users)
    {
        //do something
    }
}