error in reading data from json file

Posted by Klbaiju under ASP.NET on 10/28/2018 | Points: 10 | Views : 152 | Status : [Member] | Replies : 0
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




Responses

(No response found.)

Login to post response