程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> winform-C#字符串獲取後如何取其中的一個字符

winform-C#字符串獲取後如何取其中的一個字符

編輯:編程綜合問答
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。。。。。。。。。。。。。。。。。。

最佳回答:


_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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved