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": "someone@gmail.com",
    "mobile": "9898989898",
    "fullName": "Ramesh Sharma",
    "location": "Rajkot",
    "city_id": 1
  }
]}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
kiran girase
  • 97
  • 2
  • 13
  • 3
    That's not valid JSON. Are you sure that's your JSON, without a property? – ProgrammingLlama Jun 10 '18 at 15:35
  • _"I get the error every time"_ What is the exact error message? – CodeNotFound Jun 10 '18 at 15:37
  • I am getting this type of json data, "[{\"id\":11,\"userName\":null,\"passWord\":null,\"email\":\"someone@gmail.com\",\"mobile\":\"9423422882\",\"fullName\":\"Ramesh Sharma\",\"location\":\"Rajkot\",\"city_id\":1}]" but for removing '\' i used `var results = JsonConvert.DeserializeObject(responseString);` this code then i get that type of array – kiran girase Jun 10 '18 at 15:39
  • 1
    @kiran you can use the validator [here](https://jsonformatter.curiousconcept.com/) to see that your JSON is invalid. You ought to work on fixing that first. – ProgrammingLlama Jun 10 '18 at 15:41
  • my proper data is this `"[{\"id\":11,\"userName\":null,\"passWord\":null,\"email\":\"someone@gmail.com\",\"mobile\":\"9423422882\",\"fullName\":\"Ramesh Sharma\",\"location\":\"Rajkot\",\"city_id\":1}]"` – kiran girase Jun 10 '18 at 15:47

2 Answers2

1

In a comment to your question you said your JSON is:

[
  {
    "id": 11,
    "userName": null,
    "passWord": null,
    "email": "someone@gmail.com",
    "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);
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
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":"someone@gmail.com","mobile":"9423422882","fullName":"Ramesh Sharma","location":"Rajkot","city_id":1}]
Prany
  • 2,078
  • 2
  • 13
  • 31
  • 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 – kiran girase Jun 10 '18 at 17:45
  • @kirangirase Cheers Mate – Prany Jun 10 '18 at 18:11