Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Hello All,
I don't have any idea how to read and write Json file in c#, but in my project client requirment is to implement Json, so please help me if any body have any URL or sample code where it is given the detail steps for using Json in asp.net(C#).
 
Details like Insert/update/delete data from Json file.
 
Thanking you in advance.
Posted 10-Oct-12 2:55am
Edited 10-Oct-12 3:07am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Json are simmilar to XML in terms of use but they are equivalent to txt file in terms of accessibility. you can use teh following code to read/write a jasoin file. you need to change the code as per your requirement.
 
READ :
 
//Open the file              
var stream = File.OpenText("json file.txt"); 
//Read the file              
string st = stream.ReadToEnd();                           
var jsonArray = JsonArray.Parse(st);              
foreach (var item in jsonArray)              
{                                   
JsonObject ob = new JsonObject(item);                   
foreach (var t in ob.Values)                  
{                       
JsonObject oo = new JsonObject(t);                       
foreach (var x in oo)                      
{                          
textBox1.AppendText(x.Key + " : " + x.Value + "\n");                      
}                  
}                 
 
WRITE :
 
KeyValuePair<string, JsonValue> pair = new KeyValuePair<string, JsonValue>("FName","Sourabh");             
KeyValuePair<string, JsonValue> pair2 = new KeyValuePair<string, JsonValue>("LName", "SInha");              
List<KeyValuePair<string, JsonValue>> list = new List<KeyValuePair<string, JsonValue>>();             
list.Add(pair);             
list.Add(pair2);                          
JsonObject jObject = new JsonObject(list);             
var stream = new StreamWriter("json out file.txt");             
            
foreach (var x in jObject)             
{               
//  textBox1.AppendText(x.Key + " : " + x.Value + "\n");                 
textBox1.AppendText(x.ToString() + "\n");                 
stream.WriteLine(x.ToString() + "\n");             
}             
JsonArray jarray = new JsonArray("item1","item2","Item3");             
foreach (var x in jarray)             
{                                 
textBox1.AppendText(x.ToString());                 
stream.WriteLine(x.ToString());             
}         
 
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100