Xamarin Forms - 将 Observable 集合输出到 JSON

Xamarin Forms - Output Observable Collection to JSON

我有一个绑定到 ListView 的 Observable 集合。

Observable 集合中的内容包含产品名称、产品描述和产品价格,显示在 ListView 中。

还有其他字段,例如产品 ID 等,这些字段在 ListView 中是隐藏的,但在 Observable 集合中。

此 Observable 集合中将有多个项目。

我想将这个可观察集合输出为 JSON 包,这样我就可以通过 API.

提交它

这是我的包含可观察集合的代码。

public static ObservableCollection<FBProduct> fbproducts = new ObservableCollection<FBProduct>();

下面是我如何简单地将项目添加到 Observable 集合。

Payment.SelectedProductID = foo.id;
Payment.SelectedProductTitle = foo.title;
Payment.SelectedProductPrice = foo.price;
Payment.SelectedProductFeaturedImage = foo.featured_src;

我如何将这个 Observable 集合输出为 JSON 包,以便它显示如下内容:

"line_items": [
   {
   "product_id": 1234,
   "product_name": apple,
   "product_price": 12,
   "product_description": green apple,
   "quantity": 1
   },
   {
   "product_id": 9876,
   "product_name": pear,
   "product_price": 10,
   "product_description": green pear,
   "quantity": 1
   },
],

使用Newtonsoft

string json = JsonConvert.SerializeObject(fbproducts);