【问题标题】:Newtonsoft JSON Deserialize AND jsonfreezeNewtonsoft JSON 反序列化和 jsonfreeze
【发布时间】:2013-11-10 14:19:35
【问题描述】:

我有两个简单的 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字符串创建兼容格式?

【问题讨论】:

    标签: c# php json vb.net


    【解决方案1】:

    你可以从这里开始。您创建了一些具有代表 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);
    

    【讨论】:

    • &lt;JsonProperty("#type")&gt; _ 谢谢,成功了,我领悟了本质!
    • 哦,对不起,我不知何故完全忽略了它是 vb.net,但我很高兴它可以帮助您了解实际的想法。请随时 upvote and accept answers 帮助您并欢迎使用 StackOverflow ;-)
    • stackoverflow 服务很棒!几年来发现了很多问题和解决方法。困难在于我不是英语专业人士。我不得不使用翻译来解决一个非常重要的问题。再次感谢!
    【解决方案2】:

    "#type": "订单"

    "#type": "OrderLine",

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

    【讨论】:

      猜你喜欢
      • 2013-06-06
      • 1970-01-01
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多