我已经成功收集了json数据并将其显示在列表视图中,但似乎无法解决如何将sfdatagrid与json数据一起使用
我是c#的初级水平,所以任何帮助都会非常感激
这是我的代码
namespace Drip
{
public class RootObject
{
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; }
}
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<RootObject>(content);
_data = new ObservableCollection<Feed>(data.Feeds);
postsListView.ItemsSource = _data;
_data= new ObservableCollection<Feed>(data.Feeds);
viewModel.ItemsSource = _data;
base.OnAppearing();
}
void OnAdd(object sender, System.EventArgs e)
{
}
void OnUpdate(object sender, System.EventArgs e)
{
}
void OnDelete(object sender, System.EventArgs e)
{
}
}
}
这是我的JSON数据
{
"channel": {
"id": 301726,
"name": "Testing ESP8266",
"description": "Water meter pulse count",
"latitude": "0.0",
"longitude": "0.0",
"field1": "Water Pulse",
"created_at": "2017-07-12T12:19:38Z",
"updated_at": "2017-09-26T08:41:17Z",
"elevation": "54",
"last_entry_id": 151
},
"feeds": [
{
"created_at": "2017-08-15T13:14:28Z",
"entry_id": 52,
"field1": "13.00\r\n\r\n"
},
{
"created_at": "2017-08-15T13:14:44Z",
"entry_id": 53,
"field1": "13.00\r\n\r\n"
},
{
"created_at": "2017-08-15T13:14:59Z",
"entry_id": 54,
"field1": "13.00\r\n\r\n"
},
{
"created_at": "2017-08-15T13:15:15Z",
"entry_id": 55,
"field1": "13.00\r\n\r\n"
},
{
"created_at": "2017-08-15T13:15:31Z",
"entry_id": 56,
"field1": "13.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:41:38Z",
"entry_id": 57,
"field1": "1.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:41:54Z",
"entry_id": 58,
"field1": "8.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:42:10Z",
"entry_id": 59,
"field1": "8.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:42:26Z",
"entry_id": 60,
"field1": "8.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:42:41Z",
"entry_id": 61,
"field1": "8.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:42:57Z",
"entry_id": 62,
"field1": "11.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:43:12Z",
"entry_id": 63,
"field1": "14.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:43:27Z",
"entry_id": 64,
"field1": "14.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:43:42Z",
"entry_id": 65,
"field1": "14.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:43:58Z",
"entry_id": 66,
"field1": "14.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:44:14Z",
"entry_id": 67,
"field1": "14.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:44:29Z",
"entry_id": 68,
"field1": "14.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:44:45Z",
"entry_id": 69,
"field1": "14.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:45:15Z",
"entry_id": 70,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:45:31Z",
"entry_id": 71,
"field1": "1.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:45:46Z",
"entry_id": 72,
"field1": "3.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:46:02Z",
"entry_id": 73,
"field1": "7.00\r\n\r\n"
},
{
"created_at": "2017-09-05T12:46:18Z",
"entry_id": 74,
"field1": "7.00\r\n\r\n"
},
{
"created_at": "2017-09-07T18:11:48Z",
"entry_id": 75,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-07T18:12:05Z",
"entry_id": 76,
"field1": "5.00\r\n\r\n"
},
{
"created_at": "2017-09-07T18:12:20Z",
"entry_id": 77,
"field1": "5.00\r\n\r\n"
},
{
"created_at": "2017-09-07T18:12:36Z",
"entry_id": 78,
"field1": "5.00\r\n\r\n"
},
{
"created_at": "2017-09-07T18:12:51Z",
"entry_id": 79,
"field1": "5.00\r\n\r\n"
},
{
"created_at": "2017-09-07T18:13:06Z",
"entry_id": 80,
"field1": "5.00\r\n\r\n"
},
{
"created_at": "2017-09-07T18:13:21Z",
"entry_id": 81,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-07T18:13:37Z",
"entry_id": 82,
"field1": "3.00\r\n\r\n"
},
{
"created_at": "2017-09-07T18:13:52Z",
"entry_id": 83,
"field1": "3.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:25:31Z",
"entry_id": 84,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:25:51Z",
"entry_id": 85,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:26:08Z",
"entry_id": 86,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:26:28Z",
"entry_id": 87,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:27:00Z",
"entry_id": 88,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:27:18Z",
"entry_id": 89,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:27:34Z",
"entry_id": 90,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:27:49Z",
"entry_id": 91,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:36:06Z",
"entry_id": 92,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:36:22Z",
"entry_id": 93,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:36:39Z",
"entry_id": 94,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:36:56Z",
"entry_id": 95,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:37:11Z",
"entry_id": 96,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:37:28Z",
"entry_id": 97,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:37:44Z",
"entry_id": 98,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:37:59Z",
"entry_id": 99,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:38:37Z",
"entry_id": 100,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:38:54Z",
"entry_id": 101,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:39:21Z",
"entry_id": 102,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:39:36Z",
"entry_id": 103,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:39:55Z",
"entry_id": 104,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:40:14Z",
"entry_id": 105,
"field1": "1.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:40:35Z",
"entry_id": 106,
"field1": "1.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:40:51Z",
"entry_id": 107,
"field1": "1.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:41:08Z",
"entry_id": 108,
"field1": "1.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:41:27Z",
"entry_id": 109,
"field1": "1.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:41:42Z",
"entry_id": 110,
"field1": "1.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:48:27Z",
"entry_id": 111,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:48:43Z",
"entry_id": 112,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:49:01Z",
"entry_id": 113,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T08:49:20Z",
"entry_id": 114,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T09:01:34Z",
"entry_id": 115,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T09:02:10Z",
"entry_id": 116,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T09:02:28Z",
"entry_id": 117,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T09:02:58Z",
"entry_id": 118,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T09:11:58Z",
"entry_id": 119,
"field1": "2.00\r\n\r\n"
},
{
"created_at": "2017-09-25T09:12:13Z",
"entry_id": 120,
"field1": "3.00\r\n\r\n"
},
{
"created_at": "2017-09-25T12:56:31Z",
"entry_id": 121,
"field1": "1.00\r\n\r\n"
},
{
"created_at": "2017-09-25T12:56:47Z",
"entry_id": 122,
"field1": "9.00\r\n\r\n"
},
{
"created_at": "2017-09-25T12:57:02Z",
"entry_id": 123,
"field1": "9.00\r\n\r\n"
},
{
"created_at": "2017-09-25T12:57:17Z",
"entry_id": 124,
"field1": "9.00\r\n\r\n"
},
{
"created_at": "2017-09-25T12:57:32Z",
"entry_id": 125,
"field1": "9.00\r\n\r\n"
},
{
"created_at": "2017-09-25T13:02:46Z",
"entry_id": 126,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T13:03:03Z",
"entry_id": 127,
"field1": "2.00\r\n\r\n"
},
{
"created_at": "2017-09-25T13:03:18Z",
"entry_id": 128,
"field1": "2.00\r\n\r\n"
},
{
"created_at": "2017-09-25T13:03:34Z",
"entry_id": 129,
"field1": "2.00\r\n\r\n"
},
{
"created_at": "2017-09-25T13:03:50Z",
"entry_id": 130,
"field1": "4.00\r\n\r\n"
},
{
"created_at": "2017-09-25T13:04:06Z",
"entry_id": 131,
"field1": "6.00\r\n\r\n"
},
{
"created_at": "2017-09-25T13:04:21Z",
"entry_id": 132,
"field1": "6.00\r\n\r\n"
},
{
"created_at": "2017-09-25T13:04:36Z",
"entry_id": 133,
"field1": "6.00\r\n\r\n"
},
{
"created_at": "2017-09-25T13:31:51Z",
"entry_id": 134,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-25T13:32:06Z",
"entry_id": 135,
"field1": "1.00\r\n\r\n"
},
{
"created_at": "2017-09-25T13:32:22Z",
"entry_id": 136,
"field1": "3.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:07:30Z",
"entry_id": 137,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:07:47Z",
"entry_id": 138,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:11:52Z",
"entry_id": 139,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:12:08Z",
"entry_id": 140,
"field1": "2.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:35:34Z",
"entry_id": 141,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:35:50Z",
"entry_id": 142,
"field1": "3.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:36:07Z",
"entry_id": 143,
"field1": "3.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:36:24Z",
"entry_id": 144,
"field1": "3.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:36:40Z",
"entry_id": 145,
"field1": "3.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:36:57Z",
"entry_id": 146,
"field1": "4.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:37:13Z",
"entry_id": 147,
"field1": "6.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:37:29Z",
"entry_id": 148,
"field1": "6.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:37:45Z",
"entry_id": 149,
"field1": "6.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:41:01Z",
"entry_id": 150,
"field1": "0.00\r\n\r\n"
},
{
"created_at": "2017-09-26T08:41:17Z",
"entry_id": 151,
"field1": "0.00\r\n\r\n"
}
]
}
只需要输入id,日期和时间以及字段1,数据
谢谢
更新后的代码
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Drip;assembly=Drip"
xmlns:syncfusion="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms "
x:Class="Drip.DripPage">
<ContentPage.Content>
<syncfusion:SfDataGrid x:Name="datagGrid"
ItemsSource="{Binding _data}">
</syncfusion:SfDataGrid>
</ContentPage.Content>
</ContentPage>
using System.Net.Http;
using Newtonsoft.Json;
using Xamarin.Forms;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System;
using Syncfusion.Data;
using Syncfusion.SfDataGrid.XForms;
using System.Collections;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Drip
{
public class RootObject
{
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; }
}
public partial class DripPage : ContentPage
{
private const string Url = "https://thingspeak.com/channels/301726/field/1.json";
private HttpClient _client = new HttpClient();
private ObservableCollection<Feed> _data;
private SfDataGrid grid;
}
protected override async void OnAppearing()
{
var content = await _client.GetStringAsync(Url);
var data = JsonConvert.DeserializeObject<RootObject>(content);
_data = new ObservableCollection<Feed>(data.Feeds);
// postsListView.ItemsSource = _data;
grid.ItemsSource = _data;
base.OnAppearing();
}
void OnAdd(object sender, System.EventArgs e)
{
}
void OnUpdate(object sender, System.EventArgs e)
{
}
void OnDelete(object sender, System.EventArgs e)
{
}
}
}
答案 0 :(得分:0)
您在XAML和后面的代码中都定义了数据网格。您只需要执行一次
您正在XAML和后面的代码中分配ItemSource。再次,只做一次
如果您已经定义了一个尚未完成的BindingContext,则只能在XAML中进行数据绑定
您只能数据绑定到公共属性,而不是私有变量
public partial class DripPage : ContentPage
{
private const string Url = "https://thingspeak.com/channels/301726/field/1.json";
private HttpClient _client = new HttpClient();
public ObservableCollection<Feed> _data { get; set; }
protected override async void OnAppearing()
{
base.OnAppearing();
var content = await _client.GetStringAsync(Url);
var data = JsonConvert.DeserializeObject<RootObject>(content);
_data = new ObservableCollection<Feed>(data.Feeds);
this.BindingContext = this;
}
答案 1 :(得分:0)
在SfDataGrid中,JSON数据无法直接绑定。要将SfDataGrid与JSON数据绑定,您需要将JSON数据反序列化为可绑定格式。请参阅以下KB链接以了解如何反序列化JSON数据并将其绑定到SfDataGrid,
另外,我们通过将JSON数据绑定到SfDataGrid来准备样本,您可以从以下链接下载相同的内容,
示例链接:http://www.syncfusion.com/downloads/support/forum/132918/ze/GridWithJSONData-1403374775