Tily-Tao 2013-10-15 02:11 采纳率: 100%
浏览 8677
已采纳

C#字符串获取后如何取其中的一个字符

_Callback( {"Ishost":0, "items":[], "calvisitcount":[0,19,1,10,24,11,29,1,1,2,2,2,0,4,0,1,6,2,1,0,2,2,2,0,1,3,0,0,0,1,0], "modvisitcount":[{"mod":0, "todaycount":0, "totalcount":1409245}, {"mod":1, "todaycount":0, "totalcount":0}, {"mod":3, "todaycount":0, "totalcount":10}, {"mod":8, "todaycount":0, "totalcount":98}, {"mod":10, "todaycount":0, "totalcount":17}], "lastlogin":0, "twlogincount":0, "lastgettime":1381797259});

其中我想要截取第一个"todaycount":和"totalcount":中间的那个数字该怎么截取?想了好久都没想出来,脑袋进水了。。。。。。。。。。。。。。。。。。。。。。。。因为这是从网页获取的,放在string里后不知道该怎么截取,求帮助,求思路,help。。。。。。。。。。。。。。。。。。

  • 写回答

4条回答 默认 最新

  • xialxl 2013-10-15 07:38
    关注

    _Callback后面小括号 中的一整个字符串是一个JSON 格式的字符串。

    C# 如下操作就可以了
    第一步,定义类 ClassA,ClassTemp:
    public class ClassA
    {
    public int mod;
    public int todaycount;
    public int totalcount;
    }
    public class ClassTemp
    {
    public int Ishost;
    public int[] calvisitcount;
    public ClassA[] modvisitcount;
    ......
    }
    第二步,字符串反序列化为ClassTemp:
    string jsonString ={"Ishost":0, "items":[], "calvisitcount":[0,19,1,10,24,11,29,1,1,2,2,2,0,4,0,1,6,2,1,0,2,2,2,0,1,3,0,0,0,1,0], "modvisitcount":[{"mod":0, "todaycount":0, "totalcount":1409245}, {"mod":1, "todaycount":0, "totalcount":0}, {"mod":3, "todaycount":0, "totalcount":10}, {"mod":8, "todaycount":0, "totalcount":98}, {"mod":10, "todaycount":0, "totalcount":17}], "lastlogin":0, "twlogincount":0, "lastgettime":1381797259};

    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    jsonString temp = serializer.Deserialize(jsonString)

    temp 是一个强类型的对象了,就可以直接用了。

    javascript 操作如下:
    var jsonObj ={"Ishost":0, "items":[], "calvisitcount":[0,19,1,10,24,11,29,1,1,2,2,2,0,4,0,1,6,2,1,0,2,2,2,0,1,3,0,0,0,1,0], "modvisitcount":[{"mod":0, "todaycount":0, "totalcount":1409245}, {"mod":1, "todaycount":0, "totalcount":0}, {"mod":3, "todaycount":0, "totalcount":10}, {"mod":8, "todaycount":0, "totalcount":98}, {"mod":10, "todaycount":0, "totalcount":17}], "lastlogin":0, "twlogincount":0, "lastgettime":1381797259};

    jsonObj 就是你要的东西了。
    var todaycount= jsonObj.modvisitcount[0].todaycount;//todaycount等于 0
    var totalcount= jsonObj.modvisitcount[0].totalcount;//totalcount等于 1409245

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!