I've got a JArray
that represents the json substring [1,2,3]
. I'd like to turn it into an int[]
instead.
我有一个表示json子串的JArray[1,2,3]。我想把它变成一个int[]。
What's the correct way of doing this? The best way I've found so far is to do the following:
正确的做法是什么?到目前为止我发现的最好的方法是:
int[] items = new int[myJArray.Count];
int i = 0;
foreach (int item in myJArray)
{
items[i++] = item;
}
5 个解决方案
#1
47
int[] items = myJArray.Select(jv => (int)jv).ToArray();
#2
32
myJArray.ToObject<int[]>();
You can also specify HashSet, List etc.
您还可以指定HashSet、List等。
The accepted answer relies on .NET's conversion - this technique uses JSON.NET's own in addition to what .NET can provide so works with more scenarios.
被接受的答案依赖于。net的转换——该技术使用了JSON。NET本身除了。NET可以提供更多的场景。
#3
2
This is pretty weak because you have to convert back into a string, but if you are doing something quick and dirty, where the performance hit won't matter, I use the below method. I like it because I don't have to write any code to map properties between json/JObject and my POCO's.
这是非常弱的,因为您必须转换回字符串,但是如果您正在做一些快速和不干净的事情,在哪里性能受到影响并不重要,我使用下面的方法。我喜欢它,因为我不需要编写任何代码来映射json/JObject和POCO之间的属性。
public static class JsonExtensions {
public static T As<T>(this JObject jobj) {
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(jobj));
}
public static List<T> ToList<T>(this JArray jarray) {
return JsonConvert.DeserializeObject<List<T>>(JsonConvert.SerializeObject(jarray));
}
}
[Test]
public void TestDeserializeRootObject() {
var json = @"{ id: 1, name: ""Dwight"" }";
var jfoo = JsonConvert.DeserializeObject(json);
var foo = (jfoo as JObject).As<Foo>();
Assert.AreEqual(1, foo.Id);
Assert.AreEqual("Dwight", foo.Name);
}
[Test]
public void TestDeserializeArray() {
var json = @"[
{ id: 1, name: ""Dwight"" }
, { id: 2, name: ""Pam"" }
]";
var foosArr = JsonConvert.DeserializeObject(json);
Assert.IsInstanceOf<JArray>(foosArr);
Assert.AreEqual(2, (foosArr as JArray).Count);
var foos = (foosArr as JArray).ToList<Foo>();
Assert.AreEqual(2, foos.Count);
var foosDict = foos.ToDictionary(f => f.Name, f => f);
Assert.IsTrue(foosDict.ContainsKey("Dwight"));
var dwight = foosDict["Dwight"];
Assert.AreEqual(1, dwight.Id);
Assert.AreEqual("Dwight", dwight.Name);
Assert.IsTrue(foosDict.ContainsKey("Pam"));
var pam = foosDict["Pam"];
Assert.AreEqual(2, pam.Id);
Assert.AreEqual("Pam", pam.Name);
}
#4
1
A cast needed first for me:
我首先需要一个演员:
((Newtonsoft.Json.Linq.JArray)myJArray).Select(item => (int)item).ToArray()
#5
0
int[] items = new int[myJArray.Count];
for (int i=0; i < myJArray.Count;i++)
{
items[i] = (int)myJArray[i]
}
this is the fastes solution you can do. The classic for
is a bit faster than the ForEach
as you access the item by the index(the foreach behind the scene uses the IEnumerator interface)
这是你能做的快速解决方案。当您通过索引访问项目时,经典的速度比ForEach要快一些(在场景后面的ForEach使用了IEnumerator接口)
or if you prefer:
或者如果你喜欢:
JsonArray arr = JsonConvert.Import("[1,2,3,4]");
int[] nums = (int[]) arr.ToArray(typeof(int));
#1
47
int[] items = myJArray.Select(jv => (int)jv).ToArray();
#2
32
myJArray.ToObject<int[]>();
You can also specify HashSet, List etc.
您还可以指定HashSet、List等。
The accepted answer relies on .NET's conversion - this technique uses JSON.NET's own in addition to what .NET can provide so works with more scenarios.
被接受的答案依赖于。net的转换——该技术使用了JSON。NET本身除了。NET可以提供更多的场景。
#3
2
This is pretty weak because you have to convert back into a string, but if you are doing something quick and dirty, where the performance hit won't matter, I use the below method. I like it because I don't have to write any code to map properties between json/JObject and my POCO's.
这是非常弱的,因为您必须转换回字符串,但是如果您正在做一些快速和不干净的事情,在哪里性能受到影响并不重要,我使用下面的方法。我喜欢它,因为我不需要编写任何代码来映射json/JObject和POCO之间的属性。
public static class JsonExtensions {
public static T As<T>(this JObject jobj) {
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(jobj));
}
public static List<T> ToList<T>(this JArray jarray) {
return JsonConvert.DeserializeObject<List<T>>(JsonConvert.SerializeObject(jarray));
}
}
[Test]
public void TestDeserializeRootObject() {
var json = @"{ id: 1, name: ""Dwight"" }";
var jfoo = JsonConvert.DeserializeObject(json);
var foo = (jfoo as JObject).As<Foo>();
Assert.AreEqual(1, foo.Id);
Assert.AreEqual("Dwight", foo.Name);
}
[Test]
public void TestDeserializeArray() {
var json = @"[
{ id: 1, name: ""Dwight"" }
, { id: 2, name: ""Pam"" }
]";
var foosArr = JsonConvert.DeserializeObject(json);
Assert.IsInstanceOf<JArray>(foosArr);
Assert.AreEqual(2, (foosArr as JArray).Count);
var foos = (foosArr as JArray).ToList<Foo>();
Assert.AreEqual(2, foos.Count);
var foosDict = foos.ToDictionary(f => f.Name, f => f);
Assert.IsTrue(foosDict.ContainsKey("Dwight"));
var dwight = foosDict["Dwight"];
Assert.AreEqual(1, dwight.Id);
Assert.AreEqual("Dwight", dwight.Name);
Assert.IsTrue(foosDict.ContainsKey("Pam"));
var pam = foosDict["Pam"];
Assert.AreEqual(2, pam.Id);
Assert.AreEqual("Pam", pam.Name);
}
#4
1
A cast needed first for me:
我首先需要一个演员:
((Newtonsoft.Json.Linq.JArray)myJArray).Select(item => (int)item).ToArray()
#5
0
int[] items = new int[myJArray.Count];
for (int i=0; i < myJArray.Count;i++)
{
items[i] = (int)myJArray[i]
}
this is the fastes solution you can do. The classic for
is a bit faster than the ForEach
as you access the item by the index(the foreach behind the scene uses the IEnumerator interface)
这是你能做的快速解决方案。当您通过索引访问项目时,经典的速度比ForEach要快一些(在场景后面的ForEach使用了IEnumerator接口)
or if you prefer:
或者如果你喜欢:
JsonArray arr = JsonConvert.Import("[1,2,3,4]");
int[] nums = (int[]) arr.ToArray(typeof(int));