首页 > 代码库 > C# 解析嵌套的json文件.

C# 解析嵌套的json文件.

概述

  今天我同学问我如何转换json文件,没处理过,网上搜了一下,json转excel的很少,反过来倒是有许多人写了工具.

json文件的结构大致是这样的:

1 {"votes": {"funny": 0, "useful": 7, "cool": 0}, "user_id": "CR2y7yEm4X035ZMzrTtN9Q", "name": "Jim", "average_stars": 5.0, "review_count": 6, "type": "user"}2 {"votes": {"funny": 0, "useful": 1, "cool": 0}, "user_id": "_9GXoHhdxc30ujPaQwh6Ew", "name": "Kelle", "average_stars": 1.0, "review_count": 2, "type": "user"}3 {"votes": {"funny": 0, "useful": 1, "cool": 0}, "user_id": "8mM-nqxjg6pT04kwcjMbsw", "name": "Stephanie", "average_stars": 5.0, "review_count": 2, "type": "user"}4 {"votes": {"funny": 0, "useful": 2, "cool": 0}, "user_id": "Ch6CdTR2IVaVANr-RglMOg", "name": "T", "average_stars": 5.0, "review_count": 2, "type": "user"}5 {"votes": {"funny": 0, "useful": 0, "cool": 0}, "user_id": "NZrLmHRyiHmyT1JrfzkCOA", "name": "Beth", "average_stars": 1.0, "review_count": 1, "type": "user"}6 {"votes": {"funny": 30, "useful": 45, "cool": 36}, "user_id": "mWx5Sxt_dx-sYBZg6RgJHQ", "name": "Amy", "average_stars": 3.79, "review_count": 19, "type": "user"}7 {"votes": {"funny": 28, "useful": 130, "cool": 31}, "user_id": "hryUDaRk7FLuDAYui2oldw", "name": "Beach", "average_stars": 3.8300000000000001, "review_count": 207, "type": "user"}8 {"votes": {"funny": 1, "useful": 0, "cool": 1}, "user_id": "2t6fZNLtiqsihVmeO7zggg", "name": "christine", "average_stars": 3.0, "review_count": 2, "type": "user"}9 {"votes": {"funny": 0, "useful": 3, "cool": 2}, "user_id": "mn6F-eP5WU37b-iLTop2mQ", "name": "Denis", "average_stars": 4.5, "review_count": 4, "type": "user"}

我定义一个类(User),用于构造json文件对应的每行(每行对应一个User对象).

然后定义一个UserManager类去处理,返回一个List<User>,完整代码如下:

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5   6 using Json_User;  7   8 namespace Json_User {  9     //对应json每行. 10     public class User { 11         public Votes votes { get; private set; } 12  13         public string user_id { get; private set; } 14  15         public string name { get; private set; } 16  17         public double average_stars { get; private set; } 18  19         public int review_count { get; private set; } 20  21         public string type { get; private set; } 22  23         public User(Votes votes, string user_id, string name, double average_stars, int review_count, string type) { 24             this.votes = votes; 25             this.user_id = user_id; 26             this.name = name; 27             this.average_stars = average_stars; 28             this.review_count = review_count; 29             this.type = type; 30         } 31     } 32  33     public class Votes { 34         public int funny { get; private set; } 35  36         public int useful { get; private set; } 37  38         public int cool { get; private set; } 39  40         public Votes(int funny, int useful, int cool) { 41             this.funny = funny; 42             this.useful = useful; 43             this.cool = cool; 44         } 45     } 46  47     //处理类. 48     static class UserManager { 49         /// <summary> 50         /// 解析嵌套的json.如:"{"votes": {"funny": 0, "useful": 7, "cool": 0}, "user_id": "CR2y7yEm4X035ZMzrTtN9Q", "name": "Jim", "average_stars": 5.0, "review_count": 6, "type": "user"}" 51         /// </summary> 52         /// <param name="lines">没行代表一个User</param> 53         /// <returns></returns> 54         public static List<User> ParseJSONString(string[] lines) { 55             System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer(); 56             Dictionary<string, object> userDic = null; 57  58             List<User> users = new List<User>(lines.Length); 59             User u = null; 60             Votes v = null; 61  62             //为成员变量赋值. 63             int funny; 64             int useful; 65             int cool; 66             string user_id; 67             string name; 68             double average_stars; 69             int review_count; 70             string type; 71             foreach (string item in lines) { 72                 //User. 73                 userDic = ser.Deserialize<Dictionary<string, object>>(item); 74  75                 //User中的votes成员变量. 76                 Dictionary<string, object> votesDic = (Dictionary<string, object>)userDic["votes"]; 77  78                 funny = int.Parse(votesDic["funny"].ToString()); 79                 useful = int.Parse(votesDic["useful"].ToString()); 80                 cool = int.Parse(votesDic["cool"].ToString()); 81                 v = new Votes(funny, useful, cool); 82  83                 user_id = userDic["user_id"].ToString(); 84                 name = userDic["name"].ToString(); 85                 average_stars = double.Parse(userDic["average_stars"].ToString()); 86                 review_count = int.Parse(userDic["review_count"].ToString()); 87                 type = userDic["type"].ToString(); 88  89                 u = new User(v, user_id, name, average_stars, review_count, type); 90                 users.Add(u); 91             } 92             return users; 93         } 94  95         // remove "this" if not on C# 3.0 / .NET 3.5 96         public static System.Data.DataTable ConvertToDatatable<T>(this IList<T> data) { 97             System.ComponentModel.PropertyDescriptorCollection props = 98                 System.ComponentModel.TypeDescriptor.GetProperties(typeof(T)); 99             System.Data.DataTable table = new System.Data.DataTable();100             for (int i = 0; i < props.Count; i++) {101                 System.ComponentModel.PropertyDescriptor prop = props[i];102                 table.Columns.Add(prop.Name, prop.PropertyType);103             }104             object[] values = new object[props.Count];105             foreach (T item in data) {106                 for (int i = 0; i < values.Length; i++) {107                     values[i] = props[i].GetValue(item);108                 }109                 table.Rows.Add(values);110             }111             return table;112         }113     }114 }

 

C# 解析嵌套的json文件.