[英]how to read and display json data in text file using c#
我真的需要所有可用的帮助。
目标:如何在普通文本文件中读取Json Data并将特定文本放入String变量? 编码平台:Visual Studio 2010语言:C#
以下是我的普通文本文件中的示例json数据
{
"created_at":"Sun May 05 14:12:21 +0000 2013",
"id":331048726577692674,
"id_str":"331048726577692674",
"text":"Why play Luiz at CB?",
"user":
{
"id":458765935,
"id_str":"458765935",
"name":"Amrit Singhpong",
"screen_name":"AmritTheBlue",
"location":"Stamford Bridge",
"url":null,
"description":"17. Chelsea fan! XO Care Free",
}
}
现在,我所能做的就是只读取文本文件中的所有行,并将其放入我创建的用于存储每一行的数组中。 因此,假设将整个json数据示例存储在单个数组中,那么我的下一个问题是,如何取出“文本”:“为什么要在CB播放Luiz?” 并将其放入普通的字符串变量中?
您可以使用.NET JavaScriptSerializer();
{ "created_at" : "Sun May 05 14:12:21 +0000 2013",
"id" : 331048726577692674,
"id_str" : "331048726577692674",
"text" : "Why play Luiz at CB?",
"user" : { "description" : "17. Chelsea fan! XO Care Free",
"id" : 458765935,
"id_str" : "458765935",
"location" : "Stamford Bridge",
"name" : "Amrit Singhpong",
"screen_name" : "AmritTheBlue",
"url" : null
}
}
string JSON = File.ReadAllText("JSON.txt");
var serializer = new JavaScriptSerializer();
object str = serializer.DeserializeObject(JSON);
您的选择:
JavaScriptSerializer
(在框架中)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.