我似乎无法将 Json 的类排列到 Linq XML 中。
c.first
、c.second
和 c.third
突出显示并声明:
"Are you missing a using directive or assembly reference."
var serializer = new JavaScriptSerializer();
var json1 = "[count:[place:{first:1,second:2,third:3}],[place:{first:11,second:22,third:33}],[place:{first:111,second:222,third:333}]]]";
var jsons = serializer.Serialize(json1);
var jsona = serializer.Deserialize<List<jClass>>(jsons);
var xmld = new XDocument(
new XElement("count", jsona.Select(c =>
new XElement("place",
new XElement("first", c.first),
new XElement("second", c.second),
new XElement("third", c.third)
)
))
);
类.cs
public class jClass
{
public jNumber[] count { get; set; }
}
public class jNumber
{
public jTopThree[] place { get; set; }
}
public class jTopThree
{
public int first { get; set; }
public int second { get; set; }
public int third { get; set; }
}
最佳答案
你的问题是你的对象结构本质上是一个数组的数组,而你只做一个选择。在你构建 xml 的地方,你的 c
变量在 jClass
级别,所以你试图读取 first
,second
和 third
属性。
目前还不清楚你的 xml 结构应该是什么,但你要么需要使用更多的 .Select
调用来进一步深入到 jTopThree 实例,使用 .SelectMany
将其展平,或稍微更改对象定义。
关于c# - JSON 到 XML 类转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43135594/