概述
今天我同学问我如何转换json文件,没处理过,网上搜了一下,json转excel的很少,反过来倒是有许多人写了工具.
json文件的结构大致是这样的:
{"votes": {"funny": , "useful": , "cool": }, "user_id": "CR2y7yEm4X035ZMzrTtN9Q", "name": "Jim", "average_stars": 5.0, "review_count": , "type": "user"}
{"votes": {"funny": , "useful": , "cool": }, "user_id": "_9GXoHhdxc30ujPaQwh6Ew", "name": "Kelle", "average_stars": 1.0, "review_count": , "type": "user"}
{"votes": {"funny": , "useful": , "cool": }, "user_id": "8mM-nqxjg6pT04kwcjMbsw", "name": "Stephanie", "average_stars": 5.0, "review_count": , "type": "user"}
{"votes": {"funny": , "useful": , "cool": }, "user_id": "Ch6CdTR2IVaVANr-RglMOg", "name": "T", "average_stars": 5.0, "review_count": , "type": "user"}
{"votes": {"funny": , "useful": , "cool": }, "user_id": "NZrLmHRyiHmyT1JrfzkCOA", "name": "Beth", "average_stars": 1.0, "review_count": , "type": "user"}
{"votes": {"funny": , "useful": , "cool": }, "user_id": "mWx5Sxt_dx-sYBZg6RgJHQ", "name": "Amy", "average_stars": 3.79, "review_count": , "type": "user"}
{"votes": {"funny": , "useful": , "cool": }, "user_id": "hryUDaRk7FLuDAYui2oldw", "name": "Beach", "average_stars": 3.8300000000000001, "review_count": , "type": "user"}
{"votes": {"funny": , "useful": , "cool": }, "user_id": "2t6fZNLtiqsihVmeO7zggg", "name": "christine", "average_stars": 3.0, "review_count": , "type": "user"}
{"votes": {"funny": , "useful": , "cool": }, "user_id": "mn6F-eP5WU37b-iLTop2mQ", "name": "Denis", "average_stars": 4.5, "review_count": , "type": "user"}
我定义一个类(User),用于构造json文件对应的每行(每行对应一个User对象).
然后定义一个UserManager类去处理,返回一个List<User>,完整代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using Json_User; namespace Json_User {
//对应json每行.
public class User {
public Votes votes { get; private set; } public string user_id { get; private set; } public string name { get; private set; } public double average_stars { get; private set; } public int review_count { get; private set; } public string type { get; private set; } public User(Votes votes, string user_id, string name, double average_stars, int review_count, string type) {
this.votes = votes;
this.user_id = user_id;
this.name = name;
this.average_stars = average_stars;
this.review_count = review_count;
this.type = type;
}
} public class Votes {
public int funny { get; private set; } public int useful { get; private set; } public int cool { get; private set; } public Votes(int funny, int useful, int cool) {
this.funny = funny;
this.useful = useful;
this.cool = cool;
}
} //处理类.
static class UserManager {
/// <summary>
/// 解析嵌套的json.如:"{"votes": {"funny": 0, "useful": 7, "cool": 0}, "user_id": "CR2y7yEm4X035ZMzrTtN9Q", "name": "Jim", "average_stars": 5.0, "review_count": 6, "type": "user"}"
/// </summary>
/// <param name="lines">没行代表一个User</param>
/// <returns></returns>
public static List<User> ParseJSONString(string[] lines) {
System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
Dictionary<string, object> userDic = null; List<User> users = new List<User>(lines.Length);
User u = null;
Votes v = null; //为成员变量赋值.
int funny;
int useful;
int cool;
string user_id;
string name;
double average_stars;
int review_count;
string type;
foreach (string item in lines) {
//User.
userDic = ser.Deserialize<Dictionary<string, object>>(item); //User中的votes成员变量.
Dictionary<string, object> votesDic = (Dictionary<string, object>)userDic["votes"]; funny = int.Parse(votesDic["funny"].ToString());
useful = int.Parse(votesDic["useful"].ToString());
cool = int.Parse(votesDic["cool"].ToString());
v = new Votes(funny, useful, cool); user_id = userDic["user_id"].ToString();
name = userDic["name"].ToString();
average_stars = double.Parse(userDic["average_stars"].ToString());
review_count = int.Parse(userDic["review_count"].ToString());
type = userDic["type"].ToString(); u = new User(v, user_id, name, average_stars, review_count, type);
users.Add(u);
}
return users;
} // remove "this" if not on C# 3.0 / .NET 3.5
public static System.Data.DataTable ConvertToDatatable<T>(this IList<T> data) {
System.ComponentModel.PropertyDescriptorCollection props =
System.ComponentModel.TypeDescriptor.GetProperties(typeof(T));
System.Data.DataTable table = new System.Data.DataTable();
for (int i = ; i < props.Count; i++) {
System.ComponentModel.PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data) {
for (int i = ; i < values.Length; i++) {
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
}
}