C# 解析json 为对象


call: reportQuery / GetReportData
result: {
    "ReportName": "GL_BalanceSumRpt",
    "DataSource": {
        "Rows": [{
            "accountTypeName": "资产",
            "accountTypeID": "6",
            "accountCode": "1001",
            "accountID": "1",
            "accountName": "库存现金",
            "currencyID": "4",
            "[PeriodEndBalance-期末余额][periodEndBalanceOrigAmountDr-借方]": "10017.00",
            "[PeriodEndBalance-期末余额][periodEndBalanceOrigAmountCr-贷方]": "",
            "GroupLevel": "0",
            "rowType": "D",
            "reportRowType": "1"
        }]
    }
}
这个json对象有中文文字  我该怎么解析

Row字段包含特殊字符不能当做实体类属性,可以考虑用dictionary来替代,但是没有类那么方便调用属性和有ide提示了,示例代码如下

有帮助或启发麻烦点个采纳【本回答右上角】,谢谢~~

img

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var s = @"{call: ""reportQuery / GetReportData"",
result: {
    ""ReportName"": ""GL_BalanceSumRpt"",
    ""DataSource"": {
        ""Rows"": [{
            ""accountTypeName"": ""资产"",
            ""accountTypeID"": ""6"",
            ""accountCode"": ""1001"",
            ""accountID"": ""1"",
            ""accountName"": ""库存现金"",
            ""currencyID"": ""4"",
            ""[PeriodEndBalance-期末余额][periodEndBalanceOrigAmountDr-借方]"": ""10017.00"",
            ""[PeriodEndBalance-期末余额][periodEndBalanceOrigAmountCr-贷方]"": """",
            ""GroupLevel"": ""0"",
            ""rowType"": ""D"",
            ""reportRowType"": ""1""
        }]
    }
}";
            var obj = new { call = string.Empty, result = new { ReportName = string.Empty, DataSource = new { Rows = new List<Dictionary<string, string>> () } } };

            var o = JsonConvert.DeserializeAnonymousType(s, obj);

            foreach (var item in o.result.DataSource.Rows)
            {
                Console.WriteLine("[PeriodEndBalance-期末余额][periodEndBalanceOrigAmountDr-借方]:" + item["[PeriodEndBalance-期末余额][periodEndBalanceOrigAmountDr-借方]"]); 
                Console.WriteLine("[PeriodEndBalance-期末余额][periodEndBalanceOrigAmountCr-贷方]:" + item["[PeriodEndBalance-期末余额][periodEndBalanceOrigAmountCr-贷方]"]); 
            }

            Console.ReadKey();
        }
    }
}


直接解析啊,直接使用第三方类库 Newtonsoft.Json.dll,序列化即可

这跟中文有一毛钱关系吗,好像没有中文你就会解析了一样