我似乎无法将Json的类排成Linq XML。
c.first
,c.second
和c.third
会突出显示并说明:
“您是否缺少using指令或程序集引用。”
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)
)
))
);
Class.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; }
}
答案 0 :(得分:2)
你的问题是你的对象结构本质上是一个数组数组,你只做一个Select。在您构建xml的位置,c
变量位于jClass
级别,因此您尝试阅读first
,second
和third
那个属性。
目前还不清楚你的xml结构应该是什么,但你要么需要再使用几个.Select
调用来深入研究jTopThree实例,使用.SelectMany
将其展平,或稍微更改您的对象定义。
答案 1 :(得分:0)
json文本和对象结构似乎无效。你可以试试这样的......
对象结构
public class jClass
{
public jTopThree[] count { get; set; }
}
public class jTopThree
{
public int first { get; set; }
public int second { get; set; }
public int third { get; set; }
}
序列
var serializer = new JavaScriptSerializer();
var json1 = "{count:[{first:1,second:2,third:3},{first:11,second:22,third:33},{first:111,second:222,third:333}]}";
var obj = serializer.Deserialize<jClass>(json1);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(jClass));
string xmlString;
using (System.IO.StringWriter sww = new System.IO.StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sww))
{
xmlSerializer.Serialize(writer, obj);
xmlString = sww.ToString();
}
}
答案 2 :(得分:0)
var json1 = "[{\"place\":{\"first\":1,\"second\":2,\"third\":3}},{\"place\":{\"first\":11,\"second\":22,\"third\":33}},{\"place\":{\"first\":111,\"second\":222,\"third\":333}}]";
XmlDocument deserializeXmlNode;
if (json1.IndexOf("[", StringComparison.OrdinalIgnoreCase) == 0)
{
deserializeXmlNode = Newtonsoft.Json.JsonConvert.DeserializeXmlNode($"{{\"items\":{json1}}}", "source");
}
else
{
deserializeXmlNode = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json1, "source");
}
Console.WriteLine(deserializeXmlNode.OuterXml);
如果您只想复制到XML并且可以买得起Nugget包,请转到Newtonsoft.Json并修改上面的代码以满足您的需求。我有slighlty更改了输入json,因为它对http://www.json.org/无效。它应该适用于任何有效的JSON。
http://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm
答案 3 :(得分:0)
首先,你的JSON格式很糟糕,基于你的例子,我觉得它看起来应该是这样的(对我来说,#34;地点&#34;似乎多余,但我现在已经离开了它):
{
&#34;计算&#34;:[
{&#34;地点&#34;:[
{&#34;第一&#34;:1,&#34;第二&#34;:2&#34;第三&#34;:3}]},
{&#34;地点&#34;:[
{&#34;第一&#34;:11,&#34;第二&#34;:22,&#34;第三&#34; 33}]},
{&#34;地点&#34;:[
{&#34;第一&#34;:111,&#34;第二&#34;:222,&#34;第三&#34; 333}]}
]
}
您的课程应如下所示:
public class RootObject
{
public List<Count> count { get; set; }
}
public class Count
{
public List<Place> place { get; set; }
}
public class Place
{
public int first { get; set; }
public int second { get; set; }
public int third { get; set; }
}
最后,您可以通过以下方式创建XML:
var serializer = new JavaScriptSerializer();
var json = "your json string";
var result= serializer.Deserialize<RootObject>(json);
var xmld = new XDocument(
new XElement("count", result.count
.SelectMany(p => p.place)
.Select(x =>
new XElement("place",
new XElement("first", x.first),
new XElement("second", x.second),
new XElement("third", x.third)
)
))
);
输出:
<count>
<place>
<first>1</first>
<second>2</second>
<third>3</third>
</place>
<place>
<first>11</first>
<second>22</second>
<third>33</third>
</place>
<place>
<first>111</first>
<second>222</second>
<third>333</third>
</place>
</count>