如何在C#上读写Json [英] how to read/write Json on C#

查看:124
本文介绍了如何在C#上读写Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我不知道如何在c#中读写Json文件,但是在我的项目客户需求中是实现Json,所以如果任何机构有任何URL或示例代码,请为我提供详细的步骤,以帮助我.在asp.net(C#)中使用Json.

详细信息,例如,从Json文件中插入/更新/删除数据.

预先感谢您.

解决方案

Json在使用方面与XML类似,但在可访问性方面与txt文件等效.您可以使用以下代码读取/写入jasoin文件.您需要根据需要更改代码.

阅读:

 // 打开文件
 var 流= File.OpenText(" );
// 读取文件
字符串 st = stream.ReadToEnd();
 var  jsonArray = JsonArray.Parse(st);
 foreach (( jsonArray中的 var 项目> )
{
JsonObject ob =  JsonObject(item);
 foreach ( var  t  in 中的值)
{
JsonObject oo =  JsonObject(t);
 foreach ( var  x  in  oo)
{
textBox1.AppendText(x.Key + "  + x.Value +   \ n");
}
} 



写:

 KeyValuePair< string,JsonValue>对=  KeyValuePair< string,JsonValue>("   Sourabh");
KeyValuePair< string,JsonValue> pair2 =  KeyValuePair< string,JsonValue>("   SInha");
List< KeyValuePair<字符串,JsonValue>> list =  List< KeyValuePair< string,JsonValue>>();
list.Add(pair);
list.Add(pair2);
JsonObject jObject =  JsonObject(list);
 var  stream =  StreamWriter("  json out file.txt");
            
 foreach ( var  x  in  jObject中的变量)
{
//  textBox1.AppendText(x.Key +:" + x.Value +"\ n"); 
textBox1.AppendText(x.ToString()+ " );
stream.WriteLine(x.ToString()+ " );
}
JsonArray jarray =  JsonArray(" " "  Item3");
 foreach ( var  x  in 中的jarray)
{
textBox1.AppendText(x.ToString());
stream.WriteLine(x.ToString());
} 


这将为您提供一个良好的开端
使用C#获取JSON数据从网络上将其映射到.NET class => [ http://www. drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and-dynamic-to-parse-json.aspx [ 解决方案

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());             
}         


This will give you a good start
Use C# to get JSON data from the web and map it to .NET class => made easy![^]

http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and-dynamic-to-parse-json.aspx[^]


这篇关于如何在C#上读写Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆