0

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
5
  • 3
    That's not valid JSON. Are you sure that's your JSON, without a property? Commented Jun 10, 2018 at 15:35
  • "I get the error every time" What is the exact error message? Commented Jun 10, 2018 at 15:37
  • I am getting this type of json data, "[{\"id\":11,\"userName\":null,\"passWord\":null,\"email\":\"[email protected]\",\"mobile\":\"9423422882\",\"fullName\":\"Ramesh Sharma\",\"location\":\"Rajkot\",\"city_id\":1}]" but for removing '\' i used var results = JsonConvert.DeserializeObject<dynamic>(responseString); this code then i get that type of array Commented Jun 10, 2018 at 15:39
  • 1
    @kiran you can use the validator here to see that your JSON is invalid. You ought to work on fixing that first. Commented Jun 10, 2018 at 15:41
  • my proper data is this "[{\"id\":11,\"userName\":null,\"passWord\":null,\"email\":\"[email protected]\",\"mobile\":\"9423422882\",\"fullName\":\"Ramesh Sharma\",\"location\":\"Rajkot\",\"city_id\":1}]" Commented Jun 10, 2018 at 15:47

2 Answers 2

1

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

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
@kirangirase Cheers Mate
1

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

I understand it is a tool, but oh my those names suck big time
@camiloterevinto Agree. Thats why it should be changed so it makes sense in your domain.
@kirangirase What do you mean model class? I dont understand what you are saying. Sorry.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.