C#-用两个数组反序列化JSON字符串?

C#-用两个数组反序列化JSON字符串?,c#,json,deserialization,C#,Json,Deserialization,我正在使用C#检索JSON数据。JSON有两个数组,一个用于出租汽车,一个用于公司汽车,然后每辆汽车有两个数据段。JSON输出如下所示 {"companycars":[[VIN,LICENSEPLATE],[VIN,LICENSEPLATE],"rentalcars":[[VIN,LICENSEPLATE],[VIN,LICENSEPLATE]]} 我使用的是JSON.net,可以处理一个数组来反序列化为一个简单的字符串字典,比如 Dictionary<string, string>

我正在使用C#检索JSON数据。JSON有两个数组,一个用于出租汽车,一个用于公司汽车,然后每辆汽车有两个数据段。JSON输出如下所示

{"companycars":[[VIN,LICENSEPLATE],[VIN,LICENSEPLATE],"rentalcars":[[VIN,LICENSEPLATE],[VIN,LICENSEPLATE]]}
我使用的是JSON.net,可以处理一个数组来反序列化为一个简单的字符串字典,比如

Dictionary<string, string> allCars = JsonConvert.DeserializeObject<Dictionary<string, string>>(myCars);
Dictionary allCars=JsonConvert.DeserializeObject(myCars);

但是同一结果中的两个数组的示例是什么?我希望最终得到两个dictionary(string)对象。

尝试创建一个类来存储反序列化JSON的结果,如下所示:

public class Cars
{
    public List<string[]> Companycars { get; set; }
    public List<string[]> Rentalcars { get; set; }

    public Cars()
    {
        Rentalcars = new List<string[]>();
        Companycars = new List<string[]>();
    }
}
公车
{
公共列表公司cars{get;set;}
公共列表Rentalcars{get;set;}
公共汽车
{
Rentalcars=新列表();
Companycars=新列表();
}
}

string myCars=“{\'companycars\”:[[\'VIN\',\'LICENSEPLATE\'],[\'VIN\',\'LICENSEPLATE\'],\'rentalcars\':[[\'VIN\',\'LICENSEPLATE\',[\'VIN\',\'LICENSEPLATE\'];
Cars allCars=JsonConvert.DeserializeObject(myCars);
希望这有帮助


编辑:

如果不需要传递对象,可以将结果存储到匿名类型中:

var allCars = new
{
    CompanyCars = new List<string[]>(),
    RentalCars = new List<string[]>()
};

string myCars = "{\"companycars\":[[\"VIN\",\"LICENSEPLATE\"],[\"VIN\",\"LICENSEPLATE\"]],\"rentalcars\":[[\"VIN\",\"LICENSEPLATE\"],[\"VIN\",\"LICENSEPLATE\"]]}";

allCars = JsonConvert.DeserializeAnonymousType(myCars, allCars);
var allCars=new
{
CompanyCars=新列表(),
RentalCars=新列表()
};
字符串myCars=“{\'companycars\”:[[\'VIN\',\'LICENSEPLATE\'],[\'VIN\',\'LICENSEPLATE\'],\'rentalcars\':[[\'VIN\',\'LICENSEPLATE\'],[\'VIN\',\'LICENSEPLATE\'];
allCars=JsonConvert.DeserializeAnonymousType(myCars,allCars);

你的意思是喜欢字典?还是说一本公司汽车字典和一本出租汽车字典>实际上两本都可以。我只需要在某个时候得到两个字符串字典——之后我可以用多种方式解析它。@Jeremy我尝试了这个建议,但JSON.net对象仍然抛出一个错误。我尝试将其作为Dictionary allCars=JsonConvert.DeserializeObjectDictionary(myCars);这里有一个技巧:通过发出完整的type info
typenameholding.All
将您所需的结构序列化为JSON,并查看JSON的外观。这有点像逆向工程,但在这种情况下很有帮助。谢谢。我试图避免这一步,因为它们都是简单的字符串,但听起来这将是我为数不多的几个选项之一
var allCars = new
{
    CompanyCars = new List<string[]>(),
    RentalCars = new List<string[]>()
};

string myCars = "{\"companycars\":[[\"VIN\",\"LICENSEPLATE\"],[\"VIN\",\"LICENSEPLATE\"]],\"rentalcars\":[[\"VIN\",\"LICENSEPLATE\"],[\"VIN\",\"LICENSEPLATE\"]]}";

allCars = JsonConvert.DeserializeAnonymousType(myCars, allCars);