【问题标题】:To Deserialize JSON and return the Value in C#?反序列化 JSON 并返回 C# 中的值?
【发布时间】:2020-12-10 02:55:57
【问题描述】:

我正在开发 Xamarin 应用程序,我正在尝试从服务器获取 Json 数据,并且运行良好。

但是,现在我只想读取/查看“邀请”的值。


Json 值:

{"invitation":"http://example.com?c_i=eyJsYWJlbCI6Iklzc3VlciIsImltYWdlVXJsIjpudWxsLCJzZXJ2aWNlRW5kcG9pbnQiOiJodHRwOi8vNTIuNzcuMjI4LjIzNDo3MDAwIiwicm91dGluZ0tleXMiOlsiSE51N0x6MkxoZktONEZEMzM2cWdDNWticWI0dTZWRkt2NERaano4YWc1eHQiXSwicmVjaXBpZW50S2V5cyI6WyI3Sm1MMVhOSHRqSHB2WW1KS3d0ZXM2djltNk5yVUJoZW1ON3J6TnZLcGN0SyJdLCJAaWQiOiIzZjgyNWRkZC0zNjNhLTQ2YzEtYTAxNi0xMjAwY2FhZjRkNTkiLCJAdHlwZSI6ImRpZDpzb3Y6QnpDYnNOWWhNcmpIaXFaRFRVQVNIZztzcGVjL2Nvbm5lY3Rpb25zLzEuMC9pbnZpdGF0aW9uIn0="}

我收到此错误...不知道为什么?


错误信息:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Osma.Mobile.App.ViewModels.Index.IndexViewModel+RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'invitation', line 1, position 14.'

我写的代码.....


班级:

public class ConnectionJson
{
    public string Invitation { get; set; }
}

public class RootObject
{
    public List<ConnectionJson> ConnectionsJson { get; set; }
}

主代码:

   public async Task<List<RootObject>> ConnectionInvitationJson()
        {
            HttpClient hTTPClient = new HttpClient();

            Uri uri = new Uri(string.Format("http://example.com/Connections/CreateInvitationJson"));

            HttpResponseMessage response = await hTTPClient.GetAsync(uri);

            string content = await response.Content.ReadAsStringAsync();
            var Items = JsonConvert.DeserializeObject<List<RootObject>>(content);

            await DialogService.AlertAsync(Items.ToString(), "Connection Invitation Json", "Ok");

            return Items;
        }

【问题讨论】:

  • 当 JSON 没有任何数组时,为什么会有这么多 List&lt;&gt;s?
  • 这行.ToString()需要什么await DialogService.AlertAsync(Items.ToString(), "Connection Invitation Json", "Ok");
  • @SowmyadharGourishetty 在对话框中输出 json 值。
  • .ToString() 不会给你 JSON 值,而是使用这个 await DialogService.AlertAsync(content, "Connection Invitation Json", "Ok");
  • await DialogService.AlertAsync(json.Invitation, "Connection Invitation Json", "Ok"); 更新到此

标签: c# .net json xamarin serialization


【解决方案1】:

您的 Json 只是一个对象。更新代码如下

var json = JsonConvert.DeserializeObject<ConnectionJson>(content);

更新代码后,如果不想更改类结构,可以修改如下逻辑

var Items = new List<RootObject> { new List<ConnectionJson> { json }};

【讨论】:

  • 没有错误,但在对话框中的消息是"Osma.Mobile.App.ViewModels.Index.IndexViewModel+ConnectionJson'
  • 为此问题添加了评论。请检查
  • @JoeDoe,我不知道 xamarin。抱歉,我无法为您提供帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多