c# json解析 如何得到text内容 要具体代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# json解析 如何得到text内容 要具体代码相关的知识,希望对你有一定的参考价值。
"result" : "ok","tuc" : [ "authors" : [ 13 ],"meaningId" : 2397164096556578585,"phrase" : "text" : "机场","language" : "zho" ,"authors" : [ 3266 ],"meaningId" : 1269040436618011315,"phrase" : "text" : "机场","language" : "zho"]
你肯定已经装了json.NET是吧?
然后你这个字符串存成info.json文件,读取代码如下:
using System.Collections.Generic;using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace jsonproject
class Program
static void Main(string[] args)
using (StreamReader reader = File.OpenText("info.json"))
JObject oText = (JObject)JToken.ReadFrom(new JsonTextReader(reader));
var contents =
from p in oText["tuc"]
select (string)p["phrase"]["text"];
foreach (var item in contents)
Console.WriteLine(item);
System.Console.ReadKey();
追问
基本成功了,汉字不识别有乱码
追答文件要存成UTF-8格式。
参考技术A "errorCode":22000,"data":"xcode":"06b9ed05b37158341d07286be822d843","songList":["queryId":"11277008","songId":11277008,"songName":"\u52c7\u6c14","artistId":"1095","artistName":"\u6881\u9759\u8339","albumId":11276429,"albumName":"\u5979\u0026\u5979 [\u597d\u5973\u771f\u7231\u6b4c]","songPicSmall":"http:\/\/b.hiphotos.baidu.com\/ting\/pic\/item\/18d8bc3eb13533fa50437d0daad3fd1f41345b18.jpg","songPicBig":"http:\/\/a.hiphotos.baidu.com\/ting\/pic\/item\/9d82d158ccbf6c814c0bc5cbbe3eb13533fa40d1.jpg","songPicRadio":"http:\/\/c.hiphotos.baidu.com\/ting\/pic\/item\/2cf5e0fe9925bc31e1a8efbb5cdf8db1cb137018.jpg","lrcLink":"\/data2\/lrc\/11301207\/11301207.lrc","version":"","copyType":3,"time":238,"linkCode":22000,"songLink":"http:\/\/file.qianqian.com\/\/data2\/music\/42016392\/42016392.mp3?xcode=06b9ed05b3715834080e0c237f8c5ca50a27df109cddc66b\u0026src=\u0022http%3A%2F%2Fpan.baidu.com%2Fshare%2Flink%3Fshareid%3D459557%26uk%3D2503337755\u0022","showLink":"http:\/\/pan.baidu.com\/share\/link?shareid=459557\u0026uk=2503337755","format":"mp3","rate":128,"size":3824863,"relateStatus":"0","resourceType":"2"]====解析后图片:
这是WinForm项目,源中的\u52c7\u6c14等要转换为中文。 参考技术B 您先定义一个实体来类
Att_Machine_RecordModel
[Serializable]
public partial class Att_Machine_RecordModel
public Att_Machine_RecordModel()
#region Model
/// <summary>
///
/// </summary>
public int PId=0;
/// <summary>
///
/// </summary>
public string AttCode= "";
/// <summary>
///
/// </summary>
public string CheckTime= "";
/// <summary>
///
/// </summary>
public string CheckType="";
/// <summary>
///
/// </summary>
public string LastModifyTime="";
/// <summary>
///
/// </summary>
public int MachineNum=0;
/// <summary>
///
/// </summary>
public string EntityState="";
/// <summary>
///
/// </summary>
public string EntityKey ="";
#endregion Model
然后把这段文字
string _JsonText = System.IO.File.ReadAllText(@"C:\inetpub\wwwroot\test\2014-08-151.txt");
List<Att_Machine_RecordModel> list = new List<Att_Machine_RecordModel>();
System.Runtime.Serialization.Json.DataContractJsonSerializer _Json = new System.Runtime.Serialization.Json.DataContractJsonSerializer(list.GetType());
byte[] _Using = System.Text.Encoding.UTF8.GetBytes(_JsonText);
System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using);
_MemoryStream.Position = 0;
list = (List<Att_Machine_RecordModel>)_Json.ReadObject(_MemoryStream);
foreach(var v in list)
AddAtt_Machine_Record(DefaultDBString, v);
追问
这是什么。。。
追答我已经写了呀
list = (List)_Json.ReadObject(_MemoryStream);
就是转换成List,然后通过list的对象Att_Machine_RecordModel取值呀
AddAtt_Machine_Record(DefaultDBString, v);出错了
追答靠, list = (List)_Json.ReadObject(_MemoryStream);
就是您要的结果呀
实体类:
[DataContract]
public class Person
[DataMember(Order = 0, IsRequired = true)]
public string Name get; set;
[DataMember(Order = 1)]
public int Age get; set;
[DataMember(Order = 2)]
public bool Alive get; set;
[DataMember(Order = 3)]
public string[] FavoriteFilms get; set;
[DataMember(Order = 4)]
public Person Child get; set;
定义:
Action<object> log = o => Console.WriteLine(o);
Func<int, int, int> add = (x, y) => x + y;
var p1 = new Person
Age = 12,
Alive = true,
Name = "lj",
FavoriteFilms = new[] "Up", "Avatar"
;
var p2 = new Person() Age = 28, Name = "cy", Child = p1 ; 参考技术D "result" : "ok","tuc" : [ "authors" : [ 13 ],"meaningId" : 2397164096556578585,"phrase" : "text" : "机场","language" : "zho" ,"authors" : [ 3266 ],"meaningId" : 1269040436618011315,"phrase" : "text" : "机场","language" : "zho"]
如何使用 C# 将 JSON 对象解析为类
【中文标题】如何使用 C# 将 JSON 对象解析为类【英文标题】:How to parse a JSON object into class using C# 【发布时间】:2021-07-03 03:02:24 【问题描述】:我的 Json 对象
"returnStatus":"code":"0200","status":"SUCCESS","message":"The operation was successful.","requestTime":"20210407141112083","responseTime":"20210407141112256","allCities":["cityID":2622,"cityName":" UNKNOWN","cityCode":"AAA","area":"KHI"
我想解析成class.How? 如何获取cityID、CityName、cityCode、area等。
我正在尝试这段代码
allCities Cities = JsonConvert.DeserializeObject<dynamic>(response);
【问题讨论】:
好的,然后?您发布的代码有什么问题? 我们得到这种类型的错误。解析值时遇到意外字符 您发布的 json 无效(缺少"]"
这些关闭标签)。
不要使用dynamic
,除非你真的,真的必须这样做。只需创建一个代表 JSON 的类,就像 Ryan Thoma 的回答所建议的那样。
【参考方案1】:
您可以像 Ryan 之前所说的那样使用在线工具从您的 json 创建类。但我更喜欢使用 Visual Studio 中集成的“Paste Special”功能。
【讨论】:
【参考方案2】:您可以使用Json2CSharp 创建类来反序列化您的对象。如果您愿意,可以相应地重命名它们。
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class ReturnStatus
public string code get; set;
public string status get; set;
public string message get; set;
public string requestTime get; set;
public string responseTime get; set;
public class AllCity
public int cityID get; set;
public string cityName get; set;
public string cityCode get; set;
public string area get; set;
public class Root
public ReturnStatus returnStatus get; set;
public List<AllCity> allCities get; set;
然后您可以使用 myDeserializedClass.allCities 访问城市列表。
【讨论】:
以上是关于c# json解析 如何得到text内容 要具体代码的主要内容,如果未能解决你的问题,请参考以下文章