Answered by:
Error while reading a json file in C#

Question
-
User1839056048 posted
Hi,
I want to read data from a json file in asp.net
my jsonfile is user.json
{ "id": 123, "name": "Mukesh Kumar", "street": "El Camino Real", "city": "New Delhi", "zipcode": 95014 }
if I add square brackets before and after in json file ,it will not read.
my code
private string jsonFile = @"D:\MVCExampl\JsonExample\JsonExample\User.json"; protected void Page_Load(object sender, EventArgs e) { var json = File.ReadAllText(jsonFile); try { var jObject = JObject.Parse(json); if (jObject != null) { string strMsg = "<table style='border=1px solid blue; rules='All'; cellspacing='0'; border-collapse:collapse;'>"; strMsg += "<tr><td>Id</td><td>Name</td><td>Street</td><td>City</td><td>ZipCode</td></tr>"; strMsg += "<tr>"; strMsg += "<td>" + jObject["id"].ToString() + "</td>"; strMsg += "<td>" + jObject["name"].ToString() + "</td>"; strMsg += "<td>" + jObject["street"].ToString() + "</td>"; strMsg += "<td>" + jObject["city"].ToString() + "</td>"; strMsg += "<td>" + jObject["zipcode"].ToString() + "</td>"; strMsg += "</tr>"; strMsg += "</table>"; Response.Write(strMsg); } } catch (Exception ex) { } }
this is working fine .
my requirement is
1. if there is more than one record in json file how to write a for loop.
2. how to add another record in that json file.
I have tried to add a record by adding a comma and a square bracket at at starting and end of file.
but it doesn't read
How to solve this
Regards
Baiju
Sunday, October 28, 2018 12:03 PM
Answers
-
User1839056048 posted
Dear friend,
thanks for your reply.the code is working fine.following is the working code for displaying in table format
String text = File.ReadAllText(@"D:\MVCExampl\JsonExample\JsonExample\User.json"); JToken token = JToken.Parse(text); if (token.Count() > 0) { string strMsg = "<table style='border=1px solid blue; rules='All'; cellspacing='0'; border-collapse:collapse;'>"; strMsg += "<tr><td>Id</td><td>Name</td><td>Street</td><td>City</td><td>ZipCode</td></tr>"; foreach (JObject item in token) { strMsg += "<tr>"; strMsg += "<td>" + item["id"].ToString() + "</td>"; strMsg += "<td>" + item["name"].ToString() + "</td>"; strMsg += "<td>" + item["street"].ToString() + "</td>"; strMsg += "<td>" + item["city"].ToString() + "</td>"; strMsg += "<td>" + item["zipcode"].ToString() + "</td>"; strMsg += "</tr>"; } strMsg += "</table>"; Response.Write(strMsg); }
can you share the code for inserting,update and delete
Regards
Baiju
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 29, 2018 7:02 AM
All replies
-
User475983607 posted
It's much easier to assist when the actual code that causes the issue is provided along with the steps to reproduce. However, the code has several questionable design approaches. The biggest is the empty catch block. You're hiding the valuable exception error message from yourself. Given the code above, we can only guess where the bug is located.
The other issue is you are reading the JSON stream as a string rather than populating a type. See the the NewtonSoft docs which shows how to read JSON from a file with example code. I prefer to use strong types... Anyway, the openly published docs are very clear on the subject.
https://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm
Sunday, October 28, 2018 12:55 PM -
User-893317190 posted
Hi klbaiju,
If your json file has more than one records,you could also use jtoken.
protected void Page_Load(object sender, EventArgs e) { String text= File.ReadAllText(Server.MapPath("/json/user.json")); JToken token = JToken.Parse(text); foreach (JObject item in token) { Response.Write("user:" + "<br/>"); Response.Write(item["id"] + "<br/>"); Response.Write(item["name"] + "<br/>"); Response.Write(item["street"] + "<br/>"); Response.Write(item["zipcode"] + "<br/>"); } }
My json.
[ { "id": 123, "name": "Mukesh Kumar", "street": "El Camino Real", "city": "New Delhi", "zipcode": 95014 }, { "id": 456, "name": "Mkesh Kumar", "street": "l Camino Real", "city": "Nw Delhi", "zipcode": 944 } ]
The result.
Best regards,
Ackerly Xu
Monday, October 29, 2018 5:34 AM -
User1839056048 posted
Dear friend,
thanks for your reply.the code is working fine.following is the working code for displaying in table format
String text = File.ReadAllText(@"D:\MVCExampl\JsonExample\JsonExample\User.json"); JToken token = JToken.Parse(text); if (token.Count() > 0) { string strMsg = "<table style='border=1px solid blue; rules='All'; cellspacing='0'; border-collapse:collapse;'>"; strMsg += "<tr><td>Id</td><td>Name</td><td>Street</td><td>City</td><td>ZipCode</td></tr>"; foreach (JObject item in token) { strMsg += "<tr>"; strMsg += "<td>" + item["id"].ToString() + "</td>"; strMsg += "<td>" + item["name"].ToString() + "</td>"; strMsg += "<td>" + item["street"].ToString() + "</td>"; strMsg += "<td>" + item["city"].ToString() + "</td>"; strMsg += "<td>" + item["zipcode"].ToString() + "</td>"; strMsg += "</tr>"; } strMsg += "</table>"; Response.Write(strMsg); }
can you share the code for inserting,update and delete
Regards
Baiju
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 29, 2018 7:02 AM -
User-893317190 posted
Hi klbaiju,
Insert
String text= File.ReadAllText(Server.MapPath("/json/user.json")); JToken token = JToken.Parse(text); JObject jObject = new JObject(new JProperty("id", 12), new JProperty("name", "name"), new JProperty("street", "street"), new JProperty("city", "city"), new JProperty("zipcode", 954)); ((JArray)token).Add(jObject); File.WriteAllText(Server.MapPath("/json/user.json"), token.ToString());
Delete
String text= File.ReadAllText(Server.MapPath("/json/user.json")); JToken token = JToken.Parse(text); ((JArray)token).Remove(((JArray)token).Where(j => j["id"].ToString() == "123").Single()); // select the object with id 123 using link and remove it File.WriteAllText(Server.MapPath("/json/user.json"), token.ToString());
Update.
String text= File.ReadAllText(Server.MapPath("/json/user.json")); JToken token = JToken.Parse(text); ((JArray)token).Where(j => j["id"].ToString() == "12").Single()["name"]="new name"; File.WriteAllText(Server.MapPath("/json/user.json"), token.ToString());
You could go to official site to study more
https://www.newtonsoft.com/json/help/html/Introduction.htm
Best regards,
Ackerly Xu
Tuesday, October 30, 2018 6:11 AM