Newtonsoft JSON 反序列化和 jsonfreeze


Newtonsoft JSON Deserialize AND jsonfreeze

我有两个简单的PHP类

class Order{
    public $orderNo;
    public $lines = array();
    public $paid = false;
    public function addLine(OrderLine $line) {
        $this->lines[] = $line;
    }
public function setPaid($paid = true) {
        $this->paid = true;
    }
}
class OrderLine{
public function __construct($item, $amount){
    $this->item = $item;
    $this->amount = $amount;
}
    public $item;
    public $amount;
    public $options;
}

序列化对象使用 https://github.com/mindplay-dk/jsonfreeze

$json = new JsonSerializer;
$data = $json->serialize($order);

有输出:

{
  "#type": "Order",
  "orderNo": 123,
  "lines": [{
    "#type": "OrderLine",
    "item": "milk '"fuzz'"",
    "amount": 3,
    "options": null
  },{
    "#type": "OrderLine",
    "item": "cookies",
    "amount": 7,
    "options": {
      "#type": "#hash",
      "flavor": "chocolate",
      "weight": "1'/2 lb"
    }
  }],
  "paid": true
}

在 VB.NET 中发送字符串 XMLRPC

就像使用Newtonsoft JSON获得一个实时对象一样?

以及如何通过类比活 VB.net OR C# 对象的 json 字符串来创建兼容的格式?

你可以从这里开始。你创建一些类,这些类的属性表示JSON格式(未经测试的代码,就像想法一样):

public class MyData
{
    [JsonProperty("#type")]
    public string Type { get; set; }
    [JsonProperty("#orderNo")]
    public int OrderNo { get; set; 
    [JsonProperty("paid")]
    public bool Paid { get; set; }
    [JsonProperty("lines")]
    public List<MyDataLine> Lines { get; set; }
}
public class MyDataLines
{
    [JsonProperty("#type")]
    public string Type { get; set; }
    [JsonProperty("options")]
    public MyDataLinesOptions Options { get; set; }
    // ... more
}
public class MyDataLinesOptions
{
    // ... more
}

然后,您可以像这样序列化和反序列化数据:

string json = "the json data you received";
MyData myData = JsonConvert.DeserializeObject<MyData>(json);
// ...
json = JsonConvert.SerializeObject(myData);

"#type": "Order"

"#type": "订单行",

这不是属性,这是对象类型的指示