潜水的
2020-09-16 22:13
采纳率: 42.9%
浏览 71

C#字符串分割,要怎么才能把下面的字符串详细地分离出来

string straa = "{\"reqCode\":\"a622b8505e0e409da088cc4336a05075\",\"result\":\"OK\",\"message\":\"\",\"extend1\":\"\",\"extend2\":\"\",\"extend3\":\"\",\"detailList01\":[{\"extend1\":\"U4BA7C43S20347601\",\"extend2\":\"N\",\"extend3\":\"1\"},{\"extend1\":\"U4BA7C43S20347602\",\"extend2\":\"N\",\"extend3\":\"2\"},{\"extend1\":\"U4BA7C43S20347603\",\"extend2\":\"N\",\"extend3\":\"3\"},{\"extend1\":\"U4BA7C43S20347604\",\"extend2\":\"N\",\"extend3\":\"4\"},{\"extend1\":\"U4BA7C43S20347605\",\"extend2\":\"N\",\"extend3\":\"5\"},{\"extend1\":\"U4BA7C43S20347606\",\"extend2\":\"N\",\"extend3\":\"6\"},{\"extend1\":\"U4BA7C43S20347607\",\"extend2\":\"N\",\"extend3\":\"7\"},{\"extend1\":\"U4BA7C43S20347608\",\"extend2\":\"N\",\"extend3\":\"8\"},{\"extend1\":\"U4BA7C43S20347609\",\"extend2\":\"N\",\"extend3\":\"9\"},{\"extend1\":\"U4BA7C43S20347618\",\"extend2\":\"N\",\"extend3\":\"10\"},{\"extend1\":\"U4BA7C43S20347617\",\"extend2\":\"N\",\"extend3\":\"11\"},{\"extend1\":\"U4BA7C43S20347616\",\"extend2\":\"N\",\"extend3\":\"12\"},{\"extend1\":\"U4BA7C43S20347615\",\"extend2\":\"X\",\"extend3\":\"13\"},{\"extend1\":\"U4BA7C43S20347614\",\"extend2\":\"N\",\"extend3\":\"14\"},{\"extend1\":\"U4BA7C43S20347613\",\"extend2\":\"N\",\"extend3\":\"15\"},{\"extend1\":\"U4BA7C43S20347612\",\"extend2\":\"N\",\"extend3\":\"16\"},{\"extend1\":\"U4BA7C43S20347611\",\"extend2\":\"X\",\"extend3\":\"17\"},{\"extend1\":\"U4BA7C43S20347610\",\"extend2\":\"N\",\"extend3\":\"18\"}]}";


 怎么把上面的字符串详细地分割出来???
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 猿长大人 2020-09-17 14:11
    已采纳

    第一步:先通过Json字符串创建实体类

    先整理一下你的Json字符串,去掉转义符:

        class Program
        {
            private const string straa = "{\"reqCode\":\"a622b8505e0e409da088cc4336a05075\",\"result\":\"OK\",\"message\":\"\",\"extend1\":\"\",\"extend2\":\"\",\"extend3\":\"\",\"detailList01\":[{\"extend1\":\"U4BA7C43S20347601\",\"extend2\":\"N\",\"extend3\":\"1\"},{\"extend1\":\"U4BA7C43S20347602\",\"extend2\":\"N\",\"extend3\":\"2\"},{\"extend1\":\"U4BA7C43S20347603\",\"extend2\":\"N\",\"extend3\":\"3\"},{\"extend1\":\"U4BA7C43S20347604\",\"extend2\":\"N\",\"extend3\":\"4\"},{\"extend1\":\"U4BA7C43S20347605\",\"extend2\":\"N\",\"extend3\":\"5\"},{\"extend1\":\"U4BA7C43S20347606\",\"extend2\":\"N\",\"extend3\":\"6\"},{\"extend1\":\"U4BA7C43S20347607\",\"extend2\":\"N\",\"extend3\":\"7\"},{\"extend1\":\"U4BA7C43S20347608\",\"extend2\":\"N\",\"extend3\":\"8\"},{\"extend1\":\"U4BA7C43S20347609\",\"extend2\":\"N\",\"extend3\":\"9\"},{\"extend1\":\"U4BA7C43S20347618\",\"extend2\":\"N\",\"extend3\":\"10\"},{\"extend1\":\"U4BA7C43S20347617\",\"extend2\":\"N\",\"extend3\":\"11\"},{\"extend1\":\"U4BA7C43S20347616\",\"extend2\":\"N\",\"extend3\":\"12\"},{\"extend1\":\"U4BA7C43S20347615\",\"extend2\":\"X\",\"extend3\":\"13\"},{\"extend1\":\"U4BA7C43S20347614\",\"extend2\":\"N\",\"extend3\":\"14\"},{\"extend1\":\"U4BA7C43S20347613\",\"extend2\":\"N\",\"extend3\":\"15\"},{\"extend1\":\"U4BA7C43S20347612\",\"extend2\":\"N\",\"extend3\":\"16\"},{\"extend1\":\"U4BA7C43S20347611\",\"extend2\":\"X\",\"extend3\":\"17\"},{\"extend1\":\"U4BA7C43S20347610\",\"extend2\":\"N\",\"extend3\":\"18\"}]}";
            static void Main(string[] args)
            {
                JObject json = JObject.Parse(straa);
                Console.WriteLine(json.ToString());
                Console.ReadKey();
            }
        }
    

    可在网页生成也可用插件(比如:Json转C#实体类)
    图片说明

    得到以下代码:

        public class Item
        {
            public string extend1 { get; set; }
            public string extend2 { get; set; }
            public string extend3 { get; set; }
        }
    
        public class Root
        {
            public string reqCode { get; set; }
            public string result { get; set; }
            public string message { get; set; }
            public string extend1 { get; set; }
            public string extend2 { get; set; }
            public string extend3 { get; set; }
            public List<Item> detailList01 { get; set; }
        }
    

    第二步:将你的Json字符串反序列化成为对象

    最终完整代码如下:

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace JsonParse
    {
        class Program
        {
            private const string straa = "{\"reqCode\":\"a622b8505e0e409da088cc4336a05075\",\"result\":\"OK\",\"message\":\"\",\"extend1\":\"\",\"extend2\":\"\",\"extend3\":\"\",\"detailList01\":[{\"extend1\":\"U4BA7C43S20347601\",\"extend2\":\"N\",\"extend3\":\"1\"},{\"extend1\":\"U4BA7C43S20347602\",\"extend2\":\"N\",\"extend3\":\"2\"},{\"extend1\":\"U4BA7C43S20347603\",\"extend2\":\"N\",\"extend3\":\"3\"},{\"extend1\":\"U4BA7C43S20347604\",\"extend2\":\"N\",\"extend3\":\"4\"},{\"extend1\":\"U4BA7C43S20347605\",\"extend2\":\"N\",\"extend3\":\"5\"},{\"extend1\":\"U4BA7C43S20347606\",\"extend2\":\"N\",\"extend3\":\"6\"},{\"extend1\":\"U4BA7C43S20347607\",\"extend2\":\"N\",\"extend3\":\"7\"},{\"extend1\":\"U4BA7C43S20347608\",\"extend2\":\"N\",\"extend3\":\"8\"},{\"extend1\":\"U4BA7C43S20347609\",\"extend2\":\"N\",\"extend3\":\"9\"},{\"extend1\":\"U4BA7C43S20347618\",\"extend2\":\"N\",\"extend3\":\"10\"},{\"extend1\":\"U4BA7C43S20347617\",\"extend2\":\"N\",\"extend3\":\"11\"},{\"extend1\":\"U4BA7C43S20347616\",\"extend2\":\"N\",\"extend3\":\"12\"},{\"extend1\":\"U4BA7C43S20347615\",\"extend2\":\"X\",\"extend3\":\"13\"},{\"extend1\":\"U4BA7C43S20347614\",\"extend2\":\"N\",\"extend3\":\"14\"},{\"extend1\":\"U4BA7C43S20347613\",\"extend2\":\"N\",\"extend3\":\"15\"},{\"extend1\":\"U4BA7C43S20347612\",\"extend2\":\"N\",\"extend3\":\"16\"},{\"extend1\":\"U4BA7C43S20347611\",\"extend2\":\"X\",\"extend3\":\"17\"},{\"extend1\":\"U4BA7C43S20347610\",\"extend2\":\"N\",\"extend3\":\"18\"}]}";
            static void Main(string[] args)
            {
                Root root = JsonConvert.DeserializeObject<Root>(straa);
                Console.WriteLine("reqCode = " + root.reqCode);
                Console.WriteLine("result  = " + root.result);
                Console.ReadKey();
            }
        }
    
        public class Item
        {
            public string extend1 { get; set; }
            public string extend2 { get; set; }
            public string extend3 { get; set; }
        }
    
        public class Root
        {
            public string reqCode { get; set; }
            public string result { get; set; }
            public string message { get; set; }
            public string extend1 { get; set; }
            public string extend2 { get; set; }
            public string extend3 { get; set; }
            public List<Item> detailList01 { get; set; }
        }
    }
    

    执行结果:
    图片说明

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题