[英]How can I retrieve a single value from JSON in Xamarin?
我的json在下面:
{
"Total": "5",
"Success": "true",
"Results": [
{
"accStatus": "01",
"allowPositiveBal": "01",
"bumiputera": "02",
"centralClosure": "01",
"citizenship": "03",
"closeZeroBal": "01",
"currency": "MYR",
"customerType": "3",
"earlySettlement": "01",
"exceptionPayment": "01",
"gender": "03",
"glCode": "12001",
"holidayPayment": "01",
"maturityDate": "01",
"maxAge": "60",
"maxPaymentPeriod": "360",
"minAge": "18",
"minFullSettlement": "0",
"partialSettlement": "01",
"paymentFrequency": "3",
"paymentGraceDay": "3",
"paymentType": "1",
"productCode": "03",
"productName": "House Under Construction",
"race": "03",
"religion": "02",
"reschedule": "01",
"staff": "02"
},
{
"accStatus": "01",
"allowPositiveBal": "01",
"bumiputera": "02",
"centralClosure": "01",
"citizenship": "01",
"closeZeroBal": "02",
"currency": "MYR",
"customerType": "3",
"earlySettlement": "01",
"exceptionPayment": "01",
"gender": "03",
"glCode": "80000",
"holidayPayment": "02",
"maturityDate": "01",
"maxAge": "50",
"maxPaymentPeriod": "360",
"minAge": "21",
"minFullSettlement": "0",
"partialSettlement": "01",
"paymentFrequency": "3",
"paymentGraceDay": "3",
"paymentType": "1",
"productCode": "01",
"productName": "Murabahah Car Financing",
"race": "03",
"religion": "02",
"reschedule": "01",
"staff": "02"
},
{
"accStatus": "01",
"allowPositiveBal": "01",
"bumiputera": "01",
"centralClosure": "01",
"citizenship": "01",
"closeZeroBal": "01",
"currency": "BND",
"customerType": "1",
"earlySettlement": "01",
"exceptionPayment": "01",
"gender": "01",
"glCode": "12001",
"holidayPayment": "01",
"maturityDate": "01",
"maxAge": "20",
"maxPaymentPeriod": "4",
"minAge": "18",
"minFullSettlement": "5",
"partialSettlement": "01",
"paymentFrequency": "6",
"paymentGraceDay": "2",
"paymentType": "2",
"productCode": "04",
"productName": "Ytghjgj",
"race": "01",
"religion": "01",
"reschedule": "01",
"staff": "01"
}
]
}
var json = JsonValue.Parse(myStringJson);
var data = json["data"];
foreach (var dataItem in data)
{
string myValue = dataItem["myKey"]; //Here is the compilation error
//...
}
使用NuGet將Newtonsoft.Json
添加到您的項目。 這將允許您序列化和反序列化JSON。
JObject obj = JObject.Parse(JSONDATA);
int total = (int)obj["Total"];
如果您想花哨的話,還有其他方法可以做到,但是我並不那么酷,也不知道該怎么做。 如果您需要更多信息,可以訪問http://www.newtonsoft.com/json來找到文檔。
這樣嗎:
Newtonsoft.Json.Linq.JToken token = Newtonsoft.Json.Linq.JObject.Parse(json.ToString());
foreach (var i in token.SelectToken("Results"))
{
valList.Add(i["productName"].ToString());
}
通過Newtonsoft.Json
和http://jsonutils.com :
var example = JsonConvert.DeserializeObject<Example>(jsonString);
foreach (var transaction in example.Results)
{
Console.WriteLine(transaction.productName);
}
鍵入的類:
public class Result
{
public string accStatus { get; set; }
public string allowPositiveBal { get; set; }
public string bumiputera { get; set; }
public string centralClosure { get; set; }
public string citizenship { get; set; }
public string closeZeroBal { get; set; }
public string currency { get; set; }
public string customerType { get; set; }
public string earlySettlement { get; set; }
public string exceptionPayment { get; set; }
public string gender { get; set; }
public string glCode { get; set; }
public string holidayPayment { get; set; }
public string maturityDate { get; set; }
public string maxAge { get; set; }
public string maxPaymentPeriod { get; set; }
public string minAge { get; set; }
public string minFullSettlement { get; set; }
public string partialSettlement { get; set; }
public string paymentFrequency { get; set; }
public string paymentGraceDay { get; set; }
public string paymentType { get; set; }
public string productCode { get; set; }
public string productName { get; set; }
public string race { get; set; }
public string religion { get; set; }
public string reschedule { get; set; }
public string staff { get; set; }
}
public class Example
{
public string Total { get; set; }
public string Success { get; set; }
public IList<Result> Results { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.