0

I'm new to Json with C#. I`m trying to deserialize JSON string to display to into a data grid. I succeeded to get the JSON string from the server but when trying to deserialize it, it throws this exception:

Newtonsoft.Json.JsonSerializationException: Error converting value "id" to type 'Eng_Tab.JsonData'. Path '[0]', line 1, position 5. ---> System.ArgumentException: Could not cast or convert from System.String to Eng_Tab.JsonData.

This is the data class:

public class JsonData { public int id { get; set; } public string lec { get; set; } public string sec1 { get; set; } public string sec2 { get; set; } public string sec3 { get; set; } public string sec4 { get; set; } public string sec5 { get; set; } public string sec6 { get; set; } public string sec7 { get; set; } public string sec8 { get; set; } public string sec9 { get; set; } public string sec10 { get; set; }

    public int h { get; set; }
    public int h1 { get; set; }
    public int h2 { get; set; }
    public int h3 { get; set; }
    public int h4 { get; set; }
    public int h5 { get; set; }
    public int h7 { get; set; }
    public int h8 { get; set; }
    public int h9 { get; set; }
    public int h10 { get; set; }

    public int m { get; set; }
    public int m1 { get; set; }
    public int m2 { get; set; }
    public int m3 { get; set; }
    public int m4 { get; set; }
    public int m5 { get; set; }
    public int m6 { get; set; }
    public int m7 { get; set; }
    public int m8 { get; set; }
    public int m9 { get; set; }
    public int m10 { get; set; }

}

Here is the Json string:

["id":"1","h":"7","m":"0","lec":"","h1":"0","m1":"0","sec1":"","h2":"10","m2":"0","sec2":"Abdelrahman Mohamed401119343000","h3":"10","m3":"0","sec3":"Abdelrahman Mohamed401119343000","h4":"5","m4":"0","sec4":"A401119343000","h5":"5","m5":"0","sec5":"A401119343000","h6":"5","m6":"0","sec6":"A401119343000","h7":"5","m7":"0","sec7":"A401119343000","h8":"5","m8":"0","sec8":"A401119343000","h9":"18","m9":"0","sec9":"Abdelrahman Mohamed401119343000","h10":"0","m10":"0","sec10":"set sec"]

and this is my C# code:

string qry = "de=e1&id=1";

WebRequest request =WebRequest.Create("the php link"+qry);

WebResponse response = request.GetResponse();

StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.ASCII);

string jsonData = reader.ReadToEnd();

jsonData = jsonData.Replace("{", "[").Replace("}","]");

MessageBox.Show(jsonData);

List<JsonData> result = JsonConvert.DeserializeObject<List<JsonData>>(jsonData);

metroGrid1.DataSource = result;

1 Answer 1

0

You need on object of type JsonData in Json string to deserialize. Thus your JSON string should look like [{"ABC":"PQR", ...}]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.