I want to get data from this JSON array. I use some keywords but I get the error every time, How can I get the data?
JArray test1 = JArray.Parse(jsondata);
string ids = test1["id"];
if i write "id" so i'm not get ans 11
{[
{
"id": 11,
"userName": null,
"passWord": null,
"email": "[email protected]",
"mobile": "9898989898",
"fullName": "Ramesh Sharma",
"location": "Rajkot",
"city_id": 1
}
]}
Camilo Terevinto
32.2k7 gold badges95 silver badges127 bronze badges
asked Jun 10, 2018 at 15:34
2 Answers 2
You can get each value like this, then it is upto you which value you need to choose.
foreach (JObject content in test1.Children<JObject>())
{
string Id = content["id"].ToString();
string email = content["email"].ToString();
}
By the way below is your correct formatted Json.
[{"id":11,"userName":null,"passWord":null,"email":"[email protected]","mobile":"9423422882","fullName":"Ramesh Sharma","location":"Rajkot","city_id":1}]
answered Jun 10, 2018 at 16:59
Sign up to request clarification or add additional context in comments.
2 Comments
kiran girase
Thanks Prany, I get answer from foreach loop but it will iterate everytime and and I will get value but I want specific values e.g fullName,id,cityid . can you please help me how i get this
Prany
@kirangirase Cheers Mate
In a comment to your question you said your JSON is:
[
{
"id": 11,
"userName": null,
"passWord": null,
"email": "[email protected]",
"mobile": "9898989898",
"fullName": "Ramesh Sharma",
"location": "Rajkot",
"city_id": 1
}
]
Create C# classes for your JSON as shown here and you will get these classes:
public class Rootobject
{
public Class1[] Property1 { get; set; }
}
public class Class1
{
public int id { get; set; }
public object userName { get; set; }
public object passWord { get; set; }
public string email { get; set; }
public string mobile { get; set; }
public string fullName { get; set; }
public string location { get; set; }
public int city_id { get; set; }
}
Then deserialize it like this:
var results = JsonConvert.DeserializeObject<RootObject>(yourJSON);
answered Jun 10, 2018 at 16:10
3 Comments
Camilo Terevinto
I understand it is a tool, but oh my those names suck big time
CodingYoshi
@camiloterevinto Agree. Thats why it should be changed so it makes sense in your domain.
CodingYoshi
@kirangirase What do you mean model class? I dont understand what you are saying. Sorry.
lang-cs
var results = JsonConvert.DeserializeObject<dynamic>(responseString);
this code then i get that type of array"[{\"id\":11,\"userName\":null,\"passWord\":null,\"email\":\"[email protected]\",\"mobile\":\"9423422882\",\"fullName\":\"Ramesh Sharma\",\"location\":\"Rajkot\",\"city_id\":1}]"