无敌可爱的塔 2021-09-08 14:25 采纳率: 33.3%
浏览 72

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对象有中文文字  我该怎么解析
  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2021-09-08 15:07
    关注

    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();
            }
        }
    }
    
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 9月8日

悬赏问题

  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?