简体   繁体   English

如何从JSON获取字段,然后将其余的JSON作为字节发送[]

[英]How to from a JSON, get a field, and send the rest of the JSON as a byte[]

I have the following JSON 我有以下JSON

{   
   "Guid": "abc", 
   "Data": {
      "Json": [[1,42],[2,2]]
 }

And I would like to get it into this: 我想将其介绍如下:

public class TempData
{
    public string Guid { get; set; }
    public Json Data { get; set; }
}

public class Json
{
    public byte[] Data { get; set; }
}

Using the following code i get the byte[] Data null, can anyone tell me why? 使用以下代码,我得到byte []数据为null,有人可以告诉我为什么吗?

TempData data = JsonConvert.DeserializeObject<TempData>(jsonString);

Best regards 最好的祝福

You have two mistakes in the mapping between your JSON and your serialized class: 在JSON和序列化类之间的映射中,您有两个错误:

The JSON contains a property Data , which is an object with property Json whereas the class contains a property Data which has a property Data . JSON包含属性Data ,该属性是具有属性Json的对象,而类包含属性Data ,其具有属性Data To fix this, modify the class as follows: 要解决此问题,请按如下所示修改类:

public class TempData
{
    public string Guid { get; set; }
    public DataData { get; set; }
}

public class Data
{
    public byte[] Json { get; set; }
}

The Json-property in the Json-document contains an array of arrays of ints. Json文档中的Json属性包含一个由整数组成的数组。 This cannot be serialized to a byte array. 无法将其序列化为字节数组。 There are two possible options: 有两种可能的选择:

Change the Json to contain a proper byte array: 更改Json以包含适当的字节数组:

{   
   "Guid": "abc", 
   "Data": {
      "Json": [1,42,2,2]
 }

Or modify your class so it has an array of byte arrays: 或修改您的类,使其具有字节数组数组:

public class TempData
{
    public string Guid { get; set; }
    public DataData { get; set; }
}

public class Data
{
    public byte[][] Json { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM