【问题标题】:How can I retrieve a single value from JSON in Xamarin?如何从 Xamarin 中的 JSON 检索单个值?
【发布时间】:2016-10-05 11:44:28
【问题描述】:

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

【问题讨论】:

    标签: json xamarin jsonobject


    【解决方案1】:
    var json = JsonValue.Parse(myStringJson);
    var data = json["data"];
    
    foreach (var dataItem in data)
    {
        string myValue = dataItem["myKey"]; //Here is the compilation error
        //...
    }
    

    【讨论】:

      【解决方案2】:

      是这样的吗:

          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】:

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

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

        如果您想变得花哨,还有其他一些方法可以做到,但是我不是那么酷,也不知道该怎么做。如果您想了解更多信息,可以前往http://www.newtonsoft.com/json 查找文档。

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 2018-10-17
            • 2018-11-20
            • 1970-01-01
            • 2016-07-04
            • 2019-01-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多