阅读Json Value的问题

时间:2015-12-16 11:21:21

标签: c# json

我有一个使用web api的应用程序。我有以下json,我需要解析

   mobile_synch_tax_r 

测试条件的值。问题是,虽然这种方法使用Windows服务,但它不能在web api的控制器中工作。

相关的json是:

{"SYNC_DATA": {
  "mobile_synch_tax_r": [
    {
      "idtax_registration":66,
      "gender": "",
      "title": "",
      "name": "AK",
      "home_address": "",
      "state_of_origin": "A",
      "home_town": "",
      "local_government": "",
      "occupation": "",
      "company_name": "",
      "office_address": "No",
      "ministry": "",
      "market": "",
      "park": "",
      "phone_number": "",
      "email_address": "",
      "photo_url": "",
      "tax_id": "285",
      "state": null,
      "registered_by": "imported",
      "biometric_status": 0,
      "registration_type": 2,
      "group_id": 100,
      "taxpayer_password": "*6BB",
      "tax_exempt": 0,
      "active": 1,
      "disability": "",
      "rf1": 1,
      "rf2": 1,
      "rf3": 1,
      "rf4": 1,
      "rf5": 1,
      "lf1": 1,
      "lf2": 1,
      "lf3": 1,
      "lf4": 1,
      "lf5": 1,
      "registered_on": "2015",
      "drivers_license_number": "",
      "national_number": "",
      "international_passport_number": "",
      "company_rcc": "",
      "workplace_category": "",
      "office_lg": "",
      "office_city": "n",
      "parent_id": "",
      "workplace_type": 0,
      "marital_status": "",
      "nationality": "u",
      "vend_pin": 0,
      "residential_address_status": "",
      "dob": null,
      "surname": "Ad",
      "first_name": "",
      "middle_name": "",
      "utin": "",
      "last_pw_reset_by": "",
      "temp_reg": 0,
      "company_size": "",
      "business_commencement_date": null,
      "proprietor_tax_id": "",
      "business_ownership_type": "",
      "has_subsidiary": false,
      "subsidiary": false,
      "subsidiary_of": "",
      "n": "",
      "locked": false,
      "author": "",
      "synch_status": false
    }]}}

这是控制器中的代码:

         // POST api/ak
    public HttpResponseMessage Post(HttpRequestMessage value)
    {
       var someText = value.Content.ReadAsStringAsync().Result;
        Library.WriteErrorLog("Data Received" + someText);

        var objects = JObject.Parse(someText);
        Library.WriteErrorLog("Objects are" + objects);
                            if (objects != null)
                            {
                                foreach (KeyValuePair<String, JToken> app in objects)
                                {
                                    var rootName = app.Key;
                                    if (rootName == "mobile_synch_tax_r")

           {"Have your way with me"}}

代码无法检查

                   rootName=="mobile_synch_tax_r"

我怎么会错过它?

1 个答案:

答案 0 :(得分:0)

如果您只是在寻找"mobile_synch_tax_r"属性的价值,可以使用SelectToken来获取它:

var mobile_synch_tax_r = objects.SelectToken("..mobile_synch_tax_r");

..递归下降运算符,因此该查询在JSON对象层次结构中搜索名为"mobile_synch_tax_r"的第一个属性,并返回其值。