I have data of the following form:
我有以下表格的资料:
{
"sections" : [
{
"section" : {
"Term" : "News",
"Term ID" : "4,253"
}
},
{
"section" : {
"Term" : "Sports",
"Term ID" : "4,254"
}
},
// ...
]
}
I would like to serialize it into a collection of the following class:
我想将它序列化为以下类的集合:
public class Section
{
public string Name;
public int Tid;
}
Here is the code I'm using to do it, using JSON.NET:
下面是我用JSON.NET做的代码:
// e.Result is the downloaded JSON
JObject jsonData = JObject.Parse(e.Result);
var sections = jsonData["sections"].Select(obj => obj["section"]).Select(sectData => new Section()
{
Name = HttpUtility.HtmlDecode(sectData["Term"].Value<string>().Replace("\"", "")),
Tid = int.Parse(sectData["Term ID"].Value<string>().Replace(",", ""))
});
foreach (Section s in sections)
{
// _sections is an ObservableCollection<Section>
_sections.Add(s);
}
It feels a bit clunky. Can I do this more elegantly?
感觉有点笨重。我能做得更优雅吗?
Particularly that foreach
loop at the end. I'd rather use a method like addAll
or concat
or something.
尤其是最后的foreach循环。我宁愿使用addAll或concat之类的方法。
3 个解决方案
#1
2
Something along the lines of...
类似于……
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Section> sections = serializer.Deserialize<List<Sections>>(e.Result);
Also look at DataContractJsonSerializer which technically supercedes JavaScriptSerializer but it always seems to be a hassle when I try to use it.
还可以看看DataContractJsonSerializer,它在技术上超过了JavaScriptSerializer,但当我尝试使用它时,它似乎总是很麻烦。
#2
0
You don't have to use Replace
to remove the thousand separator before parsing the number, the Parse
method is capable of handling them if you just allow them and make sure that it uses a culture that actually has commas as thousands separator:
在解析这个数字之前,您不必使用Replace来删除千位分隔符,如果您只允许它们,并且确保它使用的是一个具有数千分隔符的文化,那么解析方法就能够处理它们。
Tid = Int32.Parse(sectData["Term ID"].Value<string>(), NumberStyles.AllowThousands, CultureInfo.InvariantCulture)
If the _sections
variable is a List<Section>
, then you can just use it's AddRange method to add them all at once:
如果_sections变量是List
_sections.AddRange(sections);
Alternatively, if the list is to only contain those items, you can create the list from the result instead of creating it first and then adding the items to it:
或者,如果列表只包含那些项,那么您可以从结果创建列表,而不是先创建它,然后将项目添加到它:
_sections = sections.ToList();
#3
0
I suggest that you rewrite the anonymous delegate in the Select statement as follows:
我建议您在Select语句中重写匿名委托,如下所示:
var sections = jsonData["sections"].Select(obj => obj["section"]).Select(sectData =>
{
var section = new Section()
{
Name = HttpUtility.HtmlDecode(sectData["Term"].Value<string>().Replace("\"", `enter code here`"")),
Tid = int.Parse(sectData["Term ID"].Value<string>().Replace(",", ""))
};
_sections.Add(section);
return section;
});
Remember that lambdas can form closures, so the _sections collection is available in the delegate passed to Select. This approach should get rid of the foreach loop.
请记住,lambdas可以形成闭包,因此,在传递给Select的委托中,_sections集合是可用的。这种方法应该去掉foreach循环。
#1
2
Something along the lines of...
类似于……
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Section> sections = serializer.Deserialize<List<Sections>>(e.Result);
Also look at DataContractJsonSerializer which technically supercedes JavaScriptSerializer but it always seems to be a hassle when I try to use it.
还可以看看DataContractJsonSerializer,它在技术上超过了JavaScriptSerializer,但当我尝试使用它时,它似乎总是很麻烦。
#2
0
You don't have to use Replace
to remove the thousand separator before parsing the number, the Parse
method is capable of handling them if you just allow them and make sure that it uses a culture that actually has commas as thousands separator:
在解析这个数字之前,您不必使用Replace来删除千位分隔符,如果您只允许它们,并且确保它使用的是一个具有数千分隔符的文化,那么解析方法就能够处理它们。
Tid = Int32.Parse(sectData["Term ID"].Value<string>(), NumberStyles.AllowThousands, CultureInfo.InvariantCulture)
If the _sections
variable is a List<Section>
, then you can just use it's AddRange method to add them all at once:
如果_sections变量是List
_sections.AddRange(sections);
Alternatively, if the list is to only contain those items, you can create the list from the result instead of creating it first and then adding the items to it:
或者,如果列表只包含那些项,那么您可以从结果创建列表,而不是先创建它,然后将项目添加到它:
_sections = sections.ToList();
#3
0
I suggest that you rewrite the anonymous delegate in the Select statement as follows:
我建议您在Select语句中重写匿名委托,如下所示:
var sections = jsonData["sections"].Select(obj => obj["section"]).Select(sectData =>
{
var section = new Section()
{
Name = HttpUtility.HtmlDecode(sectData["Term"].Value<string>().Replace("\"", `enter code here`"")),
Tid = int.Parse(sectData["Term ID"].Value<string>().Replace(",", ""))
};
_sections.Add(section);
return section;
});
Remember that lambdas can form closures, so the _sections collection is available in the delegate passed to Select. This approach should get rid of the foreach loop.
请记住,lambdas可以形成闭包,因此,在传递给Select的委托中,_sections集合是可用的。这种方法应该去掉foreach循环。