我写这篇文章是在于自己热于发现一些解决问题的新方式,通常我在分析json格式的数据,喜欢用正则,但往往不够理想 可能正则水平也不到位吧。呵呵。
首先拿到数据源
代码
public class ooojj
{
public userObj user { get; set; } //对象
public string is_retweet { get; set; }
public string retweet_count { get; set; }
public string retweet_followed_count { get; set; }
public string favorited { get; set; }
public Tl timeline { get; set; } //对象
public string rootReplyCount { get; set; }
public string rootReplyUserName { get; set; }
public string rootReplyText { get; set; }
public string rootReplyTruncated { get; set; }
public string rootReplySource { get; set; }
public string rootReplyCreatedAt { get; set; }
public string rootReplyRetweetCount { get; set; }
public string rootReplyUserScreenName { get; set; }
public RootActive rootActivityIcon { get; set; } //对象
public string id { get; set; }
public string source { get; set; }
public string created_at { get; set; }
public string text { get; set; }
public string truncated { get; set; }
public string rootReplyId { get; set; }
}
{
public userObj user { get; set; } //对象
public string is_retweet { get; set; }
public string retweet_count { get; set; }
public string retweet_followed_count { get; set; }
public string favorited { get; set; }
public Tl timeline { get; set; } //对象
public string rootReplyCount { get; set; }
public string rootReplyUserName { get; set; }
public string rootReplyText { get; set; }
public string rootReplyTruncated { get; set; }
public string rootReplySource { get; set; }
public string rootReplyCreatedAt { get; set; }
public string rootReplyRetweetCount { get; set; }
public string rootReplyUserScreenName { get; set; }
public RootActive rootActivityIcon { get; set; } //对象
public string id { get; set; }
public string source { get; set; }
public string created_at { get; set; }
public string text { get; set; }
public string truncated { get; set; }
public string rootReplyId { get; set; }
}
public class RootActive
{
public string isLantern1 { get; set; }
public string isLantern2 { get; set; }
public string isLantern3 { get; set; }
}
{
public string isLantern1 { get; set; }
public string isLantern2 { get; set; }
public string isLantern3 { get; set; }
}
public class Tl
{
public string Name {get;set;}
public string time{get;set;}
}
{
public string Name {get;set;}
public string time{get;set;}
}
代码
public class userObj
{
public string name{get;set;}
public string id{get;set;}
public string profile_image_url{get;set;}
public string screen_name{get;set;}
public string profile_image_url_large{get;set;}
public string profile_image_url_small{get;set;}
// public string name{get;set;}
}
{
public string name{get;set;}
public string id{get;set;}
public string profile_image_url{get;set;}
public string screen_name{get;set;}
public string profile_image_url_large{get;set;}
public string profile_image_url_small{get;set;}
// public string name{get;set;}
}
这个四个实体是用来装载他这个网易json格式的实体。 建议用http analyze工具分析它的格式,原理先分析它的json基础结构,构成装载模型,然后反序列化装载。
代码
/// <summary>
/// 将 JSON 转换为对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="s"></param>
/// <returns></returns>
public static T FromJson<T>(this string s)
{
var serializer = new JavaScriptSerializer();
return serializer.Deserialize<T>(s);
}
/// 将 JSON 转换为对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="s"></param>
/// <returns></returns>
public static T FromJson<T>(this string s)
{
var serializer = new JavaScriptSerializer();
return serializer.Deserialize<T>(s);
}
最后调用你所代码,实现你的结果
string content = FileHelper.ReadFile(@"\TextFile1.txt");
List<ooojj> list= JsonUtility.FromJson<List<ooojj>>(content);