如何从Xamarin中的JSON中检索单个值?

时间:2016-10-05 03:14:34

标签: json xamarin jsonobject

我的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"
    }
  ]
}

4 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

使用NuGet将Newtonsoft.Json添加到项目中。这将允许您序列化和反序列化JSON。

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

如果你想获得幻想,还有其他一些方法可以做到这一点,但是我并不那么酷,也不知道该怎么做。如果您想了解更多信息,可以访问http://www.newtonsoft.com/json查找文档。

答案 2 :(得分: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());
        }

答案 3 :(得分: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; }
}