你應該反序列化這個JSON類,然後做任何你想要的類對象。
您可以使用NewtonSoft.Json dll來達到此目的。如果你只想得到所有ConnectivityInfo
值,也可以使用LINQ
List<LiquiditySource> ls = obj.LookupData.LiquiditySources;
foreach(LiquiditySource liquiditySource in ls)
{
string connectivityInfo = liquiditySource.ConnectivityInfo;
}
:
public class LiquiditySource
{
public int ID { get; set; }
public object ParentID { get; set; }
public string Name { get; set; }
public object SequenceNo { get; set; }
public string Caption { get; set; }
public string ConnectivityInfo { get; set; }
}
public class LookupData
{
public List<LiquiditySource> LiquiditySources { get; set; }
}
public class SampleClass
{
public string AccessToken { get; set; }
public int LoggedInUserID { get; set; }
public LookupData LookupData { get; set; }
}
string json = "your json string";
SampleClass obj = JsonConvert.DeserializeObject<SampleClass>(json);
你可以得到這類似。
List<string> lsConnectivityInfo = obj.LookupData.LiquiditySources.Select(c => c.ConnectivityInfo).ToList();
如果你想furthur deserilize connectivityInfo
字符串對象並獲取其屬性,則必須再次反序列化到對象。 注意:您還必須修改您的JSON。
修改JSON:
{
"AccessToken": "1929c63a-9c72-4c0d-aea8-345c0b5411e9",
"LoggedInUserID": 1,
"LookupData": {
"LiquiditySources": [{
"ID": 1,
"ParentID": null,
"Name": "testname1",
"SequenceNo": null,
"Caption": "test1",
"ConnectivityInfo": "{\"Port\":11111,\"Username\":\"testuser1\",\"Password\":\"password1\"}"
}, {
"ID": 2,
"ParentID": null,
"Name": "testname2",
"SequenceNo": null,
"Caption": "test2",
"ConnectivityInfo": "{\"Port\":22222,\"Username\":\"testuser2\",\"Password\":\"password2\"}"
}]
}
}
作一次類:
public class ConnectivityInfo
{
public int Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
那麼做到這一點:
foreach (LiquiditySource liquiditySource in ls)
{
string connectivityInfo = liquiditySource.ConnectivityInfo;
ConnectivityInfo connectivityInfoObj = JsonConvert.DeserializeObject<ConnectivityInfo>(connectivityInfo);
}
也許這可以幫助你 - http://stackoverflow.com/問題/ 6620165/how-can-i-parse-json-with-c – smoksnes
無效的json,請至少提供正確的json – mybirthname
thi s被解析json數據 –