简体   繁体   中英

How can I retrieve a single value from JSON in Xamarin?

My json is below:

{
  "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
    //...
}

Use NuGet to add Newtonsoft.Json to your project. This will allow you to serialize and deserialize JSON.

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

If you want to get fancy there are some other ways to do it, however I'm not that cool and don't know how to do it. If you want more information you can go to http://www.newtonsoft.com/json to find the documentation.

Did it like this:

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

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

Via Newtonsoft.Json and http://jsonutils.com :

var example = JsonConvert.DeserializeObject<Example>(jsonString);
foreach (var transaction in example.Results)
{
    Console.WriteLine(transaction.productName);
}

Typed classes:

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM