LINQ on JArray always returning null
I'm trying to parse some Json
in Xamarin.Forms
I'm pretty new to Xamarin, though not to .net
Here's my simple dimple code
var htc = new HttpClient();
var rsp = await htc.GetStringAsync("myurl.com");
JArray lists = JArray.Parse(rsp);
var c = lists.Count();
var l = lists.ToList();
var w=lists.Where(x => true);
Even though c returns the correct count of items in the list, l & w are both null
How come? and how do I fix it?
Thanks!
PS. What I'm really trying to do is bind a ListView
to a JArray
, but it seems impossible directly,(Text={Binding MyPropertyName}
crashes the app). so I'm trying to run a Select
on the JArray
to convert to a KeyValuePair
. If you have any ideas to bind directly, that would be best!
UPDATE
The issue seems even odder
I tried this
var kvlist = new List<KeyValuePair<string, string>>();
foreach (JObject ll in lists)
{
kvlist.Add(new KeyValuePair<string, string>(ll["Name"].ToString(), ll["Name"].ToString()));
}
Here at least the iteration works nicely, but the kvlist is null the entire time. Trying to evaluate the kvlist variable, I get:
Unable to cast object of type 'System.RuntimeType' to type
'Mono.Debugger.Soft.TypeMirror'.
What can the matter be?
Thanks again!
json xamarin.forms
add a comment |
I'm trying to parse some Json
in Xamarin.Forms
I'm pretty new to Xamarin, though not to .net
Here's my simple dimple code
var htc = new HttpClient();
var rsp = await htc.GetStringAsync("myurl.com");
JArray lists = JArray.Parse(rsp);
var c = lists.Count();
var l = lists.ToList();
var w=lists.Where(x => true);
Even though c returns the correct count of items in the list, l & w are both null
How come? and how do I fix it?
Thanks!
PS. What I'm really trying to do is bind a ListView
to a JArray
, but it seems impossible directly,(Text={Binding MyPropertyName}
crashes the app). so I'm trying to run a Select
on the JArray
to convert to a KeyValuePair
. If you have any ideas to bind directly, that would be best!
UPDATE
The issue seems even odder
I tried this
var kvlist = new List<KeyValuePair<string, string>>();
foreach (JObject ll in lists)
{
kvlist.Add(new KeyValuePair<string, string>(ll["Name"].ToString(), ll["Name"].ToString()));
}
Here at least the iteration works nicely, but the kvlist is null the entire time. Trying to evaluate the kvlist variable, I get:
Unable to cast object of type 'System.RuntimeType' to type
'Mono.Debugger.Soft.TypeMirror'.
What can the matter be?
Thanks again!
json xamarin.forms
Which .NET JSON parsing library is being used? Perhaps the relevant tag should be added.
– Zev Spitz
Nov 30 '18 at 9:45
kvlist
isnull
the entire time -- The call to.Add
falls with a null-reference exception?
– Zev Spitz
Nov 30 '18 at 9:48
add a comment |
I'm trying to parse some Json
in Xamarin.Forms
I'm pretty new to Xamarin, though not to .net
Here's my simple dimple code
var htc = new HttpClient();
var rsp = await htc.GetStringAsync("myurl.com");
JArray lists = JArray.Parse(rsp);
var c = lists.Count();
var l = lists.ToList();
var w=lists.Where(x => true);
Even though c returns the correct count of items in the list, l & w are both null
How come? and how do I fix it?
Thanks!
PS. What I'm really trying to do is bind a ListView
to a JArray
, but it seems impossible directly,(Text={Binding MyPropertyName}
crashes the app). so I'm trying to run a Select
on the JArray
to convert to a KeyValuePair
. If you have any ideas to bind directly, that would be best!
UPDATE
The issue seems even odder
I tried this
var kvlist = new List<KeyValuePair<string, string>>();
foreach (JObject ll in lists)
{
kvlist.Add(new KeyValuePair<string, string>(ll["Name"].ToString(), ll["Name"].ToString()));
}
Here at least the iteration works nicely, but the kvlist is null the entire time. Trying to evaluate the kvlist variable, I get:
Unable to cast object of type 'System.RuntimeType' to type
'Mono.Debugger.Soft.TypeMirror'.
What can the matter be?
Thanks again!
json xamarin.forms
I'm trying to parse some Json
in Xamarin.Forms
I'm pretty new to Xamarin, though not to .net
Here's my simple dimple code
var htc = new HttpClient();
var rsp = await htc.GetStringAsync("myurl.com");
JArray lists = JArray.Parse(rsp);
var c = lists.Count();
var l = lists.ToList();
var w=lists.Where(x => true);
Even though c returns the correct count of items in the list, l & w are both null
How come? and how do I fix it?
Thanks!
PS. What I'm really trying to do is bind a ListView
to a JArray
, but it seems impossible directly,(Text={Binding MyPropertyName}
crashes the app). so I'm trying to run a Select
on the JArray
to convert to a KeyValuePair
. If you have any ideas to bind directly, that would be best!
UPDATE
The issue seems even odder
I tried this
var kvlist = new List<KeyValuePair<string, string>>();
foreach (JObject ll in lists)
{
kvlist.Add(new KeyValuePair<string, string>(ll["Name"].ToString(), ll["Name"].ToString()));
}
Here at least the iteration works nicely, but the kvlist is null the entire time. Trying to evaluate the kvlist variable, I get:
Unable to cast object of type 'System.RuntimeType' to type
'Mono.Debugger.Soft.TypeMirror'.
What can the matter be?
Thanks again!
json xamarin.forms
json xamarin.forms
edited Nov 28 '18 at 14:26
Yisroel M. Olewski
asked Nov 28 '18 at 13:23
Yisroel M. OlewskiYisroel M. Olewski
64211531
64211531
Which .NET JSON parsing library is being used? Perhaps the relevant tag should be added.
– Zev Spitz
Nov 30 '18 at 9:45
kvlist
isnull
the entire time -- The call to.Add
falls with a null-reference exception?
– Zev Spitz
Nov 30 '18 at 9:48
add a comment |
Which .NET JSON parsing library is being used? Perhaps the relevant tag should be added.
– Zev Spitz
Nov 30 '18 at 9:45
kvlist
isnull
the entire time -- The call to.Add
falls with a null-reference exception?
– Zev Spitz
Nov 30 '18 at 9:48
Which .NET JSON parsing library is being used? Perhaps the relevant tag should be added.
– Zev Spitz
Nov 30 '18 at 9:45
Which .NET JSON parsing library is being used? Perhaps the relevant tag should be added.
– Zev Spitz
Nov 30 '18 at 9:45
kvlist
is null
the entire time -- The call to .Add
falls with a null-reference exception?– Zev Spitz
Nov 30 '18 at 9:48
kvlist
is null
the entire time -- The call to .Add
falls with a null-reference exception?– Zev Spitz
Nov 30 '18 at 9:48
add a comment |
3 Answers
3
active
oldest
votes
You should not directly call .ToList
on object
type of JArray
rather you should Select
List
of type you need. For ex.
var l = lists.Select(c => new MyList
{
Item1 = c.Value<int>("ItemName1"),
Item2 = c.Value<string>("ItemName2")
}).ToList(); //Replce MyList with your class name
On the second case where w
is null
, after getting list l
you need to specify attribute, based of what you are using where
clause. For ex.
var w=l.Where(x =>x.isAdmin==true); //l is list you selected above
Hope it help you.
Thanks. This is actually how i started off. but the result is always null. So I posted some simpler variations. Any Ideas?
– Yisroel M. Olewski
Nov 28 '18 at 14:28
In answer I am selectingItemName1
,ItemName1
this should available in yourJArray
lists & need to give accurate type as well likeint
string
. Have you created class with same fields whatJArray
returning?
– CGPA6.4
Nov 28 '18 at 14:31
Hi. It never goes into the iteration, it just skips to the next line. I guess thats becaus lists is null. Tough I dont know why. Did you see what i wrote about the Debugger error?
– Yisroel M. Olewski
Nov 28 '18 at 15:58
add a comment |
Solution:
You can use code below to convert a JArray
to a list<T>
:
List<T> t =lists.ToObject<List<T>>();
Refer: https://www.newtonsoft.com/json/help/html/ToObjectType.htm
You could also use JsonConvert.DeserializeObject
to convert it directly into the desired type. You have to define a jsonModel
class with the same structure of your json
fisrtly.
List<jsonModel> modelList = JsonConvert.DeserializeObject<List<jsonModel>>(jsonStr);
Refer :https://www.newtonsoft.com/json/help/html/SerializingCollections.htm
add a comment |
The documentation seems to indicate that JArray has properties for .Count, but no overload method because it does not implement IEnumerable, however as alluded to in the comments, it does implement the JToken type (which JArray is a collection of) and implements IEnumerable.
See the following documentation for JToken: https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JToken.htm
and JArray respectively:
https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JArray.htm
The preferred mechanism is to create a strong type and then run .ToObject();
You can access JArray.ChildrenTokens which may help
Hi. Thanks.ToObject
seems to work for a single item, is there a way to convert aJArray
to a list<T>? thanks.
– Yisroel M. Olewski
Nov 28 '18 at 14:27
I think this is what you're looking for: stackoverflow.com/questions/13565245/… :). Just a note, the KeyValuePair struct goes a bit funny with JSON, as fundamentally objects are notated as a kvp. Often in web apps you have to create a strong type to prevent the key being prop name. To some this is desired behaviour.
– Neal
Nov 29 '18 at 6:25
I thinkCount
,First
andLast
are the only such properties (and they're not methods, so they can't even take aPredicate<JToken>
like the standardCount
,First
andLast
. All other LINQ methods apply becauseJArray
implementsIEnumerable<JToken>
.
– Zev Spitz
Nov 30 '18 at 9:52
You are absolutely correct, they exist as properties much like on List<T>.Count property, but also List<T> has the Count method overload due to IEnumerable<T>, whereas the JToken can itself be an array, that's why I suspect IEnumerable is implemented on this level as opposed to JArray. Will update answer.
– Neal
Nov 30 '18 at 11:36
Just to remove any ambiguity it implements IEnumerable<JToken>, and has a property available of ChildrenTokens.
– Neal
Nov 30 '18 at 11:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53520468%2flinq-on-jarray-always-returning-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should not directly call .ToList
on object
type of JArray
rather you should Select
List
of type you need. For ex.
var l = lists.Select(c => new MyList
{
Item1 = c.Value<int>("ItemName1"),
Item2 = c.Value<string>("ItemName2")
}).ToList(); //Replce MyList with your class name
On the second case where w
is null
, after getting list l
you need to specify attribute, based of what you are using where
clause. For ex.
var w=l.Where(x =>x.isAdmin==true); //l is list you selected above
Hope it help you.
Thanks. This is actually how i started off. but the result is always null. So I posted some simpler variations. Any Ideas?
– Yisroel M. Olewski
Nov 28 '18 at 14:28
In answer I am selectingItemName1
,ItemName1
this should available in yourJArray
lists & need to give accurate type as well likeint
string
. Have you created class with same fields whatJArray
returning?
– CGPA6.4
Nov 28 '18 at 14:31
Hi. It never goes into the iteration, it just skips to the next line. I guess thats becaus lists is null. Tough I dont know why. Did you see what i wrote about the Debugger error?
– Yisroel M. Olewski
Nov 28 '18 at 15:58
add a comment |
You should not directly call .ToList
on object
type of JArray
rather you should Select
List
of type you need. For ex.
var l = lists.Select(c => new MyList
{
Item1 = c.Value<int>("ItemName1"),
Item2 = c.Value<string>("ItemName2")
}).ToList(); //Replce MyList with your class name
On the second case where w
is null
, after getting list l
you need to specify attribute, based of what you are using where
clause. For ex.
var w=l.Where(x =>x.isAdmin==true); //l is list you selected above
Hope it help you.
Thanks. This is actually how i started off. but the result is always null. So I posted some simpler variations. Any Ideas?
– Yisroel M. Olewski
Nov 28 '18 at 14:28
In answer I am selectingItemName1
,ItemName1
this should available in yourJArray
lists & need to give accurate type as well likeint
string
. Have you created class with same fields whatJArray
returning?
– CGPA6.4
Nov 28 '18 at 14:31
Hi. It never goes into the iteration, it just skips to the next line. I guess thats becaus lists is null. Tough I dont know why. Did you see what i wrote about the Debugger error?
– Yisroel M. Olewski
Nov 28 '18 at 15:58
add a comment |
You should not directly call .ToList
on object
type of JArray
rather you should Select
List
of type you need. For ex.
var l = lists.Select(c => new MyList
{
Item1 = c.Value<int>("ItemName1"),
Item2 = c.Value<string>("ItemName2")
}).ToList(); //Replce MyList with your class name
On the second case where w
is null
, after getting list l
you need to specify attribute, based of what you are using where
clause. For ex.
var w=l.Where(x =>x.isAdmin==true); //l is list you selected above
Hope it help you.
You should not directly call .ToList
on object
type of JArray
rather you should Select
List
of type you need. For ex.
var l = lists.Select(c => new MyList
{
Item1 = c.Value<int>("ItemName1"),
Item2 = c.Value<string>("ItemName2")
}).ToList(); //Replce MyList with your class name
On the second case where w
is null
, after getting list l
you need to specify attribute, based of what you are using where
clause. For ex.
var w=l.Where(x =>x.isAdmin==true); //l is list you selected above
Hope it help you.
answered Nov 28 '18 at 14:12
CGPA6.4CGPA6.4
2,94731132
2,94731132
Thanks. This is actually how i started off. but the result is always null. So I posted some simpler variations. Any Ideas?
– Yisroel M. Olewski
Nov 28 '18 at 14:28
In answer I am selectingItemName1
,ItemName1
this should available in yourJArray
lists & need to give accurate type as well likeint
string
. Have you created class with same fields whatJArray
returning?
– CGPA6.4
Nov 28 '18 at 14:31
Hi. It never goes into the iteration, it just skips to the next line. I guess thats becaus lists is null. Tough I dont know why. Did you see what i wrote about the Debugger error?
– Yisroel M. Olewski
Nov 28 '18 at 15:58
add a comment |
Thanks. This is actually how i started off. but the result is always null. So I posted some simpler variations. Any Ideas?
– Yisroel M. Olewski
Nov 28 '18 at 14:28
In answer I am selectingItemName1
,ItemName1
this should available in yourJArray
lists & need to give accurate type as well likeint
string
. Have you created class with same fields whatJArray
returning?
– CGPA6.4
Nov 28 '18 at 14:31
Hi. It never goes into the iteration, it just skips to the next line. I guess thats becaus lists is null. Tough I dont know why. Did you see what i wrote about the Debugger error?
– Yisroel M. Olewski
Nov 28 '18 at 15:58
Thanks. This is actually how i started off. but the result is always null. So I posted some simpler variations. Any Ideas?
– Yisroel M. Olewski
Nov 28 '18 at 14:28
Thanks. This is actually how i started off. but the result is always null. So I posted some simpler variations. Any Ideas?
– Yisroel M. Olewski
Nov 28 '18 at 14:28
In answer I am selecting
ItemName1
, ItemName1
this should available in your JArray
lists & need to give accurate type as well like int
string
. Have you created class with same fields what JArray
returning?– CGPA6.4
Nov 28 '18 at 14:31
In answer I am selecting
ItemName1
, ItemName1
this should available in your JArray
lists & need to give accurate type as well like int
string
. Have you created class with same fields what JArray
returning?– CGPA6.4
Nov 28 '18 at 14:31
Hi. It never goes into the iteration, it just skips to the next line. I guess thats becaus lists is null. Tough I dont know why. Did you see what i wrote about the Debugger error?
– Yisroel M. Olewski
Nov 28 '18 at 15:58
Hi. It never goes into the iteration, it just skips to the next line. I guess thats becaus lists is null. Tough I dont know why. Did you see what i wrote about the Debugger error?
– Yisroel M. Olewski
Nov 28 '18 at 15:58
add a comment |
Solution:
You can use code below to convert a JArray
to a list<T>
:
List<T> t =lists.ToObject<List<T>>();
Refer: https://www.newtonsoft.com/json/help/html/ToObjectType.htm
You could also use JsonConvert.DeserializeObject
to convert it directly into the desired type. You have to define a jsonModel
class with the same structure of your json
fisrtly.
List<jsonModel> modelList = JsonConvert.DeserializeObject<List<jsonModel>>(jsonStr);
Refer :https://www.newtonsoft.com/json/help/html/SerializingCollections.htm
add a comment |
Solution:
You can use code below to convert a JArray
to a list<T>
:
List<T> t =lists.ToObject<List<T>>();
Refer: https://www.newtonsoft.com/json/help/html/ToObjectType.htm
You could also use JsonConvert.DeserializeObject
to convert it directly into the desired type. You have to define a jsonModel
class with the same structure of your json
fisrtly.
List<jsonModel> modelList = JsonConvert.DeserializeObject<List<jsonModel>>(jsonStr);
Refer :https://www.newtonsoft.com/json/help/html/SerializingCollections.htm
add a comment |
Solution:
You can use code below to convert a JArray
to a list<T>
:
List<T> t =lists.ToObject<List<T>>();
Refer: https://www.newtonsoft.com/json/help/html/ToObjectType.htm
You could also use JsonConvert.DeserializeObject
to convert it directly into the desired type. You have to define a jsonModel
class with the same structure of your json
fisrtly.
List<jsonModel> modelList = JsonConvert.DeserializeObject<List<jsonModel>>(jsonStr);
Refer :https://www.newtonsoft.com/json/help/html/SerializingCollections.htm
Solution:
You can use code below to convert a JArray
to a list<T>
:
List<T> t =lists.ToObject<List<T>>();
Refer: https://www.newtonsoft.com/json/help/html/ToObjectType.htm
You could also use JsonConvert.DeserializeObject
to convert it directly into the desired type. You have to define a jsonModel
class with the same structure of your json
fisrtly.
List<jsonModel> modelList = JsonConvert.DeserializeObject<List<jsonModel>>(jsonStr);
Refer :https://www.newtonsoft.com/json/help/html/SerializingCollections.htm
answered Nov 29 '18 at 6:28
Jack Hua - MSFTJack Hua - MSFT
1,169129
1,169129
add a comment |
add a comment |
The documentation seems to indicate that JArray has properties for .Count, but no overload method because it does not implement IEnumerable, however as alluded to in the comments, it does implement the JToken type (which JArray is a collection of) and implements IEnumerable.
See the following documentation for JToken: https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JToken.htm
and JArray respectively:
https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JArray.htm
The preferred mechanism is to create a strong type and then run .ToObject();
You can access JArray.ChildrenTokens which may help
Hi. Thanks.ToObject
seems to work for a single item, is there a way to convert aJArray
to a list<T>? thanks.
– Yisroel M. Olewski
Nov 28 '18 at 14:27
I think this is what you're looking for: stackoverflow.com/questions/13565245/… :). Just a note, the KeyValuePair struct goes a bit funny with JSON, as fundamentally objects are notated as a kvp. Often in web apps you have to create a strong type to prevent the key being prop name. To some this is desired behaviour.
– Neal
Nov 29 '18 at 6:25
I thinkCount
,First
andLast
are the only such properties (and they're not methods, so they can't even take aPredicate<JToken>
like the standardCount
,First
andLast
. All other LINQ methods apply becauseJArray
implementsIEnumerable<JToken>
.
– Zev Spitz
Nov 30 '18 at 9:52
You are absolutely correct, they exist as properties much like on List<T>.Count property, but also List<T> has the Count method overload due to IEnumerable<T>, whereas the JToken can itself be an array, that's why I suspect IEnumerable is implemented on this level as opposed to JArray. Will update answer.
– Neal
Nov 30 '18 at 11:36
Just to remove any ambiguity it implements IEnumerable<JToken>, and has a property available of ChildrenTokens.
– Neal
Nov 30 '18 at 11:44
add a comment |
The documentation seems to indicate that JArray has properties for .Count, but no overload method because it does not implement IEnumerable, however as alluded to in the comments, it does implement the JToken type (which JArray is a collection of) and implements IEnumerable.
See the following documentation for JToken: https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JToken.htm
and JArray respectively:
https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JArray.htm
The preferred mechanism is to create a strong type and then run .ToObject();
You can access JArray.ChildrenTokens which may help
Hi. Thanks.ToObject
seems to work for a single item, is there a way to convert aJArray
to a list<T>? thanks.
– Yisroel M. Olewski
Nov 28 '18 at 14:27
I think this is what you're looking for: stackoverflow.com/questions/13565245/… :). Just a note, the KeyValuePair struct goes a bit funny with JSON, as fundamentally objects are notated as a kvp. Often in web apps you have to create a strong type to prevent the key being prop name. To some this is desired behaviour.
– Neal
Nov 29 '18 at 6:25
I thinkCount
,First
andLast
are the only such properties (and they're not methods, so they can't even take aPredicate<JToken>
like the standardCount
,First
andLast
. All other LINQ methods apply becauseJArray
implementsIEnumerable<JToken>
.
– Zev Spitz
Nov 30 '18 at 9:52
You are absolutely correct, they exist as properties much like on List<T>.Count property, but also List<T> has the Count method overload due to IEnumerable<T>, whereas the JToken can itself be an array, that's why I suspect IEnumerable is implemented on this level as opposed to JArray. Will update answer.
– Neal
Nov 30 '18 at 11:36
Just to remove any ambiguity it implements IEnumerable<JToken>, and has a property available of ChildrenTokens.
– Neal
Nov 30 '18 at 11:44
add a comment |
The documentation seems to indicate that JArray has properties for .Count, but no overload method because it does not implement IEnumerable, however as alluded to in the comments, it does implement the JToken type (which JArray is a collection of) and implements IEnumerable.
See the following documentation for JToken: https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JToken.htm
and JArray respectively:
https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JArray.htm
The preferred mechanism is to create a strong type and then run .ToObject();
You can access JArray.ChildrenTokens which may help
The documentation seems to indicate that JArray has properties for .Count, but no overload method because it does not implement IEnumerable, however as alluded to in the comments, it does implement the JToken type (which JArray is a collection of) and implements IEnumerable.
See the following documentation for JToken: https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JToken.htm
and JArray respectively:
https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JArray.htm
The preferred mechanism is to create a strong type and then run .ToObject();
You can access JArray.ChildrenTokens which may help
edited Nov 30 '18 at 11:39
answered Nov 28 '18 at 13:30
NealNeal
293111
293111
Hi. Thanks.ToObject
seems to work for a single item, is there a way to convert aJArray
to a list<T>? thanks.
– Yisroel M. Olewski
Nov 28 '18 at 14:27
I think this is what you're looking for: stackoverflow.com/questions/13565245/… :). Just a note, the KeyValuePair struct goes a bit funny with JSON, as fundamentally objects are notated as a kvp. Often in web apps you have to create a strong type to prevent the key being prop name. To some this is desired behaviour.
– Neal
Nov 29 '18 at 6:25
I thinkCount
,First
andLast
are the only such properties (and they're not methods, so they can't even take aPredicate<JToken>
like the standardCount
,First
andLast
. All other LINQ methods apply becauseJArray
implementsIEnumerable<JToken>
.
– Zev Spitz
Nov 30 '18 at 9:52
You are absolutely correct, they exist as properties much like on List<T>.Count property, but also List<T> has the Count method overload due to IEnumerable<T>, whereas the JToken can itself be an array, that's why I suspect IEnumerable is implemented on this level as opposed to JArray. Will update answer.
– Neal
Nov 30 '18 at 11:36
Just to remove any ambiguity it implements IEnumerable<JToken>, and has a property available of ChildrenTokens.
– Neal
Nov 30 '18 at 11:44
add a comment |
Hi. Thanks.ToObject
seems to work for a single item, is there a way to convert aJArray
to a list<T>? thanks.
– Yisroel M. Olewski
Nov 28 '18 at 14:27
I think this is what you're looking for: stackoverflow.com/questions/13565245/… :). Just a note, the KeyValuePair struct goes a bit funny with JSON, as fundamentally objects are notated as a kvp. Often in web apps you have to create a strong type to prevent the key being prop name. To some this is desired behaviour.
– Neal
Nov 29 '18 at 6:25
I thinkCount
,First
andLast
are the only such properties (and they're not methods, so they can't even take aPredicate<JToken>
like the standardCount
,First
andLast
. All other LINQ methods apply becauseJArray
implementsIEnumerable<JToken>
.
– Zev Spitz
Nov 30 '18 at 9:52
You are absolutely correct, they exist as properties much like on List<T>.Count property, but also List<T> has the Count method overload due to IEnumerable<T>, whereas the JToken can itself be an array, that's why I suspect IEnumerable is implemented on this level as opposed to JArray. Will update answer.
– Neal
Nov 30 '18 at 11:36
Just to remove any ambiguity it implements IEnumerable<JToken>, and has a property available of ChildrenTokens.
– Neal
Nov 30 '18 at 11:44
Hi. Thanks.
ToObject
seems to work for a single item, is there a way to convert a JArray
to a list<T>? thanks.– Yisroel M. Olewski
Nov 28 '18 at 14:27
Hi. Thanks.
ToObject
seems to work for a single item, is there a way to convert a JArray
to a list<T>? thanks.– Yisroel M. Olewski
Nov 28 '18 at 14:27
I think this is what you're looking for: stackoverflow.com/questions/13565245/… :). Just a note, the KeyValuePair struct goes a bit funny with JSON, as fundamentally objects are notated as a kvp. Often in web apps you have to create a strong type to prevent the key being prop name. To some this is desired behaviour.
– Neal
Nov 29 '18 at 6:25
I think this is what you're looking for: stackoverflow.com/questions/13565245/… :). Just a note, the KeyValuePair struct goes a bit funny with JSON, as fundamentally objects are notated as a kvp. Often in web apps you have to create a strong type to prevent the key being prop name. To some this is desired behaviour.
– Neal
Nov 29 '18 at 6:25
I think
Count
, First
and Last
are the only such properties (and they're not methods, so they can't even take a Predicate<JToken>
like the standard Count
, First
and Last
. All other LINQ methods apply because JArray
implements IEnumerable<JToken>
.– Zev Spitz
Nov 30 '18 at 9:52
I think
Count
, First
and Last
are the only such properties (and they're not methods, so they can't even take a Predicate<JToken>
like the standard Count
, First
and Last
. All other LINQ methods apply because JArray
implements IEnumerable<JToken>
.– Zev Spitz
Nov 30 '18 at 9:52
You are absolutely correct, they exist as properties much like on List<T>.Count property, but also List<T> has the Count method overload due to IEnumerable<T>, whereas the JToken can itself be an array, that's why I suspect IEnumerable is implemented on this level as opposed to JArray. Will update answer.
– Neal
Nov 30 '18 at 11:36
You are absolutely correct, they exist as properties much like on List<T>.Count property, but also List<T> has the Count method overload due to IEnumerable<T>, whereas the JToken can itself be an array, that's why I suspect IEnumerable is implemented on this level as opposed to JArray. Will update answer.
– Neal
Nov 30 '18 at 11:36
Just to remove any ambiguity it implements IEnumerable<JToken>, and has a property available of ChildrenTokens.
– Neal
Nov 30 '18 at 11:44
Just to remove any ambiguity it implements IEnumerable<JToken>, and has a property available of ChildrenTokens.
– Neal
Nov 30 '18 at 11:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53520468%2flinq-on-jarray-always-returning-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Which .NET JSON parsing library is being used? Perhaps the relevant tag should be added.
– Zev Spitz
Nov 30 '18 at 9:45
kvlist
isnull
the entire time -- The call to.Add
falls with a null-reference exception?– Zev Spitz
Nov 30 '18 at 9:48