c#无法反序列化当前的JSON对象

时间:2017-09-27 07:29:49

标签: c# android c json xamarin

尝试从ThingSpeak获取JSON数据然而得到上述错误, 适用于JSON占位符作为URL。

这是我的主要cs代码:

vue-cli init

这是我的JSON:

namespace Drip
{
    public class Feed
    {
        public DateTime Created_at { get; set; }
        public int Entry_id { get; set; }
        public string Field1 { get; set; }
    }

    public partial class DripPage : TabbedPage
    {
        private const string Url = "https://thingspeak.com/channels/301726/field/1.json";
        private HttpClient _client = new HttpClient();
        private ObservableCollection<Feed> _data;

        public DripPage()
        {
            InitializeComponent();
        }

        protected override async void OnAppearing()
        {
            var content = await _client.GetStringAsync(Url);
            var data = JsonConvert.DeserializeObject<List<Feed>>(content);

            _data = new ObservableCollection<Feed>(data);
            postsListView.ItemsSource = _data;
            base.OnAppearing();
        }

2 个答案:

答案 0 :(得分:1)

返回的json不是数组,因此无法将其反序列化为List<Feed>,因此会抛出异常。它是一个对象,其中一个对象的成员是您感兴趣的数组。反序列化的后备C#类应该包含以下成员:

public class RootObject
{
    public Channel channel { get; set; }
    public List<Feed> feeds { get; set; }
}

public class Channel
{
    public int id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string latitude { get; set; }
    public string longitude { get; set; }
    public string field1 { get; set; }
    public DateTime created_at { get; set; }
    public DateTime updated_at { get; set; }
    public string elevation { get; set; }
    public int last_entry_id { get; set; }
}

public class Feed
{
    public DateTime created_at { get; set; }
    public int entry_id { get; set; }
    public string field1 { get; set; }
}

您可以反序列化为List<Feed>(或您选择称之为的任何内容),而不是反序列化为RootObject

var data = JsonConvert.DeserializeObject<RootObject>(content);
_data = new ObservableCollection<Feed>(data.feeds);

答案 1 :(得分:-1)

尝试这可能它会对你有用 首先:将此代码放在您的活动上并实现所有var;您将创建一个javalist来反序列化您的json,然后您将使用方法webclient获取您的json,然后您将使用runonuithread方法在后台执行该过程。

articles = new JavaList<RootObject>();
        mWebClient = new WebClient();
        mUrl = new Uri(urlAddress);
        mWebClient.DownloadDataAsync("https://thingspeak.com/channels/301726/field/1.json");
        mWebClient.DownloadDataCompleted += MWebClient_DownloadDataCompleted;

第二:方法mWebClient.DownloadDataCompleted将生成:

private void MWebClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
    {
        RunOnUiThread(() =>
        {
            try
            {
                string json = Encoding.UTF8.GetString(e.Result);
                articles = JsonConvert.DeserializeObject<JavaList<RootObject>>(json);
                mAdapter = new ArticleAdapterCostum(this, articles);
                mListView.Adapter = mAdapter;
            }
            catch (Exception exception)
            {
                Toast.MakeText(this, " Vueillez verifier votre connexion a internet puis reessayer ", ToastLength.Short).Show();
            }
        });
    }

不要忘记为列表视图创建一个costum适配器。