Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Newtonsoft.Json.Linq.JArray' does not contain a definition

I am trying this code:

string s = "[{status:1,fields:[{name:'n1',value:'v1'}]}]";
dynamic o = JsonConvert.DeserializeObject(s);
var f = o.fields[0].name;  

but line 3 gives this error, how come? How do you get this data?

like image 762
Control Freak Avatar asked Aug 24 '14 16:08

Control Freak


2 Answers

o is an array; you need to get the first element from it:

o[0].fields[0].name
like image 180
SLaks Avatar answered Sep 25 '22 22:09

SLaks


It should be

 string s = "[{status:1,fields:[{name:'n1',value:'v1'}]}]";
 dynamic o = JsonConvert.DeserializeObject(s);
 var f = o[0].fields[0].name;  

Here o is the array object which holds elements and you need the first one

like image 21
Chandan Kumar Avatar answered Sep 23 '22 22:09

Chandan Kumar