2016-10-05 40 views
0

我的JSON是如下:如何從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" 
    } 
    ] 
} 

回答

0
var json = JsonValue.Parse(myStringJson); 
var data = json["data"]; 

foreach (var dataItem in data) 
{ 
    string myValue = dataItem["myKey"]; //Here is the compilation error 
    //... 
} 
0

使用的NuGet添加Newtonsoft.Json到您的項目。這將允許您序列化和反序列化JSON。

JObject obj = JObject.Parse(JSONDATA); 
int total = (int)obj["Total"]; 

如果你想獲得幻想也有一些其他的方法來做到這一點,但我沒那麼涼,不知道該怎麼做。如果你想了解更多信息,你可以去http://www.newtonsoft.com/json找到文檔。

0

難道這樣的:

Newtonsoft.Json.Linq.JToken token = Newtonsoft.Json.Linq.JObject.Parse(json.ToString()); 

     foreach (var i in token.SelectToken("Results")) 
     { 
      valList.Add(i["productName"].ToString()); 
     } 
0

通過Newtonsoft.Jsonhttp://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; } 
}