0

我有一个复杂的对象如下

public class Cart
{
  public int cartID{get; set;}
  public bool IsActive{get; set;}
  public double price{get; set;}
  public List<Items> items{get; set;}
}

public class Item
{
  public int itemID{get; set;}}
  public string itemName{get;set;}
  public double price{get; set;}
}

我的服务接受如下 json

public class ServiceRequest
{
 public string Data{get;set;}
 public string ActionToBePerformed{get;set;}
}

其中 ActionToBePerformed - 指示要执行的操作。
          Data-是对象Cart的序列化列表

如何在 windows phone 7 中将 Cart 对象序列化为字符串并将其作为对象 ServiceRequest 的数据发送到服务?(不使用任何第三方 JSON 库作为 JSON.net)

4

1 回答 1

0

(不使用任何第三方 JSON 库作为 JSON.net)

然后你应该编写你自己的序列化程序——但这只是再次发明一个轮子。

你知道JSON结构是:

数组:[]等

如果您需要序列化 ​​2 个对象 - action 和 dta,那么它是一个包含 2 个对象的数组:

[{
    "Data":
    {
    "*Name of your cartID on the service*": cartID, (value from C# class)
    "*Name of your IsActive on the service*": IsActive, (value from C# class)
    "*Name of your price on the service*": price, (value from C# class)
    "*Name of your List<Items> on the service*":
    [{
        List<Items>
    }]
    }
    "ActionToBePerformed": Here if it is just a string
    {
         Here if it is a class like data
    }
}]

因此,如果您知道 JSON 应该是什么样子,那么您可以使用一些 FOR 来编写它

只需在您的列表或其他任何内容中找到所需的 CART 并写入:

string JSON="[{"Data":"服务上您的购物车 ID 的名称": + " 购物车 ID,(来自 C# 类的值)等等...

我怎么说它发明了一个轮子...

于 2013-10-01T06:30:35.460 回答