c# json解析 如何得到text内容 要具体代码
{"result":"ok","tuc":[{"authors":[13],"meaningId":2397164096556578585,"phrase":{"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格式。
展开全部
{"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等要转换为中文。
====解析后图片:
这是WinForm项目,源中的\u52c7\u6c14等要转换为中文。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
您先定义一个实体来类
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);
}
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取值呀
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
准备数据
实体类:
[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 };
实体类:
[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 };
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
{"result" : "ok","tuc" : [ {"authors" : [ 13 ],"meaningId" : 2397164096556578585,"phrase" : {"text" : "机场","language" : "zho" }},{"authors" : [ 3266 ],"meaningId" : 1269040436618011315,"phrase" : {"text" : "机场","language" : "zho"}}]
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询