2

私はjsonとc#に少し慣れていないので、APIからいくつかのデータを収集しようとしています。jsonデータへの部分的なリンクは次のとおりです:https://data.cityofchicago.org/api/views/*REMOVED*/rows.json?search=rahm&jsonp=? そして与えられたデータは少し厄介です:

{
  "meta" : {
    "view" : {
      "id" : "xzkq-xp2w",
      "name" : "Current Employee Names, Salaries, and Position Titles",
      "attribution" : "City of Chicago",
      "attributionLink" : "http://www.cityofchicago.org",
      "averageRating" : 0,
      "category" : "Administration & Finance",
      "createdAt" : 1317154735,
      "description" : "This dataset is a listing of all current City of Chicago employees, complete with full names, departments, positions, and annual salaries. For hourly employees the annual salary is estimated. Data Owner: Human Resources. Frequency: Data is updated quarterly. Last Updated: April 19, 2012. For information on the positions and related salaries detailed in the budget as of January 1, 2012, visit the \"Budget - Positions and Salaries in 2012 Appropriation Ordinance\" dataset: http://bit.ly/twq5oO",
      "displayType" : "table",
      "downloadCount" : 4033,
      "numberOfComments" : 0,
      "oid" : 538938,
      "publicationAppendEnabled" : false,
      "publicationDate" : 1334860835,
      "publicationGroup" : 241512,
      "publicationStage" : "published",
      "rowClass" : "",
      "rowsUpdatedAt" : 1334860755,
      "rowsUpdatedBy" : "scy9-9wg4",
      "searchString" : "rahm",
      "signed" : false,
      "tableId" : 300029,
      "totalTimesRated" : 0,
      "viewCount" : 48059,
      "viewLastModified" : 1334860836,
      "viewType" : "tabular",
      "columns" : [ {
        "id" : -1,
        "name" : "sid",
        "dataTypeName" : "meta_data",
        "fieldName" : "sid",
        "position" : 0,
        "renderTypeName" : "meta_data",
        "format" : {
        }
      }, {
        "id" : -1,
        "name" : "id",
        "dataTypeName" : "meta_data",
        "fieldName" : "id",
        "position" : 0,
        "renderTypeName" : "meta_data",
        "format" : {
        }
      }, {
        "id" : -1,
        "name" : "position",
        "dataTypeName" : "meta_data",
        "fieldName" : "position",
        "position" : 0,
        "renderTypeName" : "meta_data",
        "format" : {
        }
      }, {
        "id" : -1,
        "name" : "created_at",
        "dataTypeName" : "meta_data",
        "fieldName" : "created_at",
        "position" : 0,
        "renderTypeName" : "meta_data",
        "format" : {
        }
      }, {
        "id" : -1,
        "name" : "created_meta",
        "dataTypeName" : "meta_data",
        "fieldName" : "created_meta",
        "position" : 0,
        "renderTypeName" : "meta_data",
        "format" : {
        }
      }, {
        "id" : -1,
        "name" : "updated_at",
        "dataTypeName" : "meta_data",
        "fieldName" : "updated_at",
        "position" : 0,
        "renderTypeName" : "meta_data",
        "format" : {
        }
      }, {
        "id" : -1,
        "name" : "updated_meta",
        "dataTypeName" : "meta_data",
        "fieldName" : "updated_meta",
        "position" : 0,
        "renderTypeName" : "meta_data",
        "format" : {
        }
      }, {
        "id" : -1,
        "name" : "meta",
        "dataTypeName" : "meta_data",
        "fieldName" : "meta",
        "position" : 0,
        "renderTypeName" : "meta_data",
        "format" : {
        }
      }, {
        "id" : 6676081,
        "name" : "Name",
        "dataTypeName" : "text",
        "fieldName" : "name",
        "position" : 1,
        "renderTypeName" : "text",
        "tableColumnId" : 1532233,
        "width" : 148,
        "cachedContents" : {
          "non_null" : 32925,
          "smallest" : "AARON,  ELVIA J",
          "null" : 0,
          "largest" : "ZYSKOWSKI,  DARIUSZ",
          "top" : [ {
            "count" : 20,
            "item" : "ZYSKO,  RICHARD"
          }, {
            "count" : 19,
            "item" : "ZYSKOWSKI,  DARIUSZ"
          } ]
        },
        "format" : {
        }
      }, {
        "id" : 6676082,
        "name" : "Position Title",
        "dataTypeName" : "text",
        "fieldName" : "job_titles",
        "position" : 2,
        "renderTypeName" : "text",
        "tableColumnId" : 1532235,
        "width" : 220,
        "cachedContents" : {
          "non_null" : 32925,
          "smallest" : "A/MGR COM SVC-ELECTIONS",
          "null" : 0,
          "largest" : "ZONING PLAN EXAMINER",
          "top" : [ {
            "count" : 20,
            "item" : "POLICE OFFICER"
          }, {
            "count" : 19,
            "item" : "FIREFIGHTER"
          }, {
            "count" : 18,
            "item" : "MOTOR TRUCK DRIVER"
          }, {
            "count" : 17,
            "item" : "SANITATION LABORER"
          }, {
            "count" : 16,
            "item" : "FIREFIGHTER-EMT"
          }, {
            "count" : 15,
            "item" : "POLICE OFFICER (ASSIGNED AS DETECTIVE)"
          }, {
            "count" : 14,
            "item" : "SERGEANT"
          }, {
            "count" : 13,
            "item" : "CROSSING GUARD"
          }, {
            "count" : 12,
            "item" : "TRAFFIC CONTROL AIDE-HOURLY"
          }, {
            "count" : 11,
            "item" : "POOL MOTOR TRUCK DRIVER"
          }, {
            "count" : 10,
            "item" : "CONSTRUCTION LABORER"
          }, {
            "count" : 9,
            "item" : "FIREFIGHTER/PARAMEDIC"
          }, {
            "count" : 8,
            "item" : "LIBRARIAN I"
          }, {
            "count" : 7,
            "item" : "OPERATING ENGINEER-GROUP C"
          }, {
            "count" : 6,
            "item" : "PARAMEDIC"
          }, {
            "count" : 5,
            "item" : "FRM OF MACHINISTS - AUTOMOTIVE"
          }, {
            "count" : 4,
            "item" : "ELECTRICAL MECHANIC"
          }, {
            "count" : 3,
            "item" : "SENIOR PUBLIC INFORMATION OFFICER"
          }, {
            "count" : 2,
            "item" : "LEGAL SECRETARY"
          }, {
            "count" : 1,
            "item" : "AIRPORT OPERATIONS SUPVSR I"
          } ]
        },
        "format" : {
          "align" : "left"
        }
      }, {
        "id" : 6676083,
        "name" : "Department",
        "dataTypeName" : "text",
        "fieldName" : "department",
        "position" : 3,
        "renderTypeName" : "text",
        "tableColumnId" : 1532236,
        "width" : 183,
        "cachedContents" : {
          "non_null" : 32925,
          "smallest" : "ADMIN HEARNG",
          "null" : 0,
          "largest" : "WATER MGMNT",
          "top" : [ {
            "count" : 20,
            "item" : "WATER MGMNT"
          }, {
            "count" : 19,
            "item" : "POLICE"
          }, {
            "count" : 18,
            "item" : "GENERAL SERVICES"
          }, {
            "count" : 17,
            "item" : "FIRE"
          }, {
            "count" : 16,
            "item" : "FAMILY & SUPPORT"
          }, {
            "count" : 15,
            "item" : "STREETS & SAN"
          }, {
            "count" : 14,
            "item" : "FINANCE"
          }, {
            "count" : 13,
            "item" : "BUSINESS AFFAIRS"
          }, {
            "count" : 12,
            "item" : "OEMC"
          }, {
            "count" : 11,
            "item" : "TRANSPORTN"
          }, {
            "count" : 10,
            "item" : "HEALTH"
          }, {
            "count" : 9,
            "item" : "AVIATION"
          }, {
            "count" : 8,
            "item" : "LAW"
          }, {
            "count" : 7,
            "item" : "PUBLIC LIBRARY"
          }, {
            "count" : 6,
            "item" : "ADMIN HEARNG"
          }, {
            "count" : 5,
            "item" : "CULTURAL AFFAIRS"
          }, {
            "count" : 4,
            "item" : "COMMUNITY DEVELOPMENT"
          }, {
            "count" : 3,
            "item" : "BUILDINGS"
          }, {
            "count" : 2,
            "item" : "CITY COUNCIL"
          }, {
            "count" : 1,
            "item" : "CITY CLERK"
          } ]
        },
        "format" : {
        }
      }, {
        "id" : 6676084,
        "name" : "Employee Annual Salary",
        "dataTypeName" : "money",
        "fieldName" : "employee_annual_salary",
        "position" : 4,
        "renderTypeName" : "money",
        "tableColumnId" : 1532237,
        "width" : 161,
        "cachedContents" : {
          "non_null" : 32925,
          "smallest" : "0.96",
          "sum" : "2475198578.40",
          "null" : 0,
          "average" : "75176.87405922551",
          "largest" : "260004.00",
          "top" : [ {
            "count" : 20,
            "item" : "75372.00"
          }, {
            "count" : 19,
            "item" : "78012.00"
          }, {
            "count" : 18,
            "item" : "80724.00"
          }, {
            "count" : 17,
            "item" : "70408.00"
          }, {
            "count" : 16,
            "item" : "68203.20"
          }, {
            "count" : 15,
            "item" : "83706.00"
          }, {
            "count" : 14,
            "item" : "73216.00"
          }, {
            "count" : 13,
            "item" : "18886.40"
          }, {
            "count" : 12,
            "item" : "84032.00"
          }, {
            "count" : 11,
            "item" : "86840.00"
          }, {
            "count" : 10,
            "item" : "61530.00"
          }, {
            "count" : 9,
            "item" : "83982.00"
          }, {
            "count" : 8,
            "item" : "99648.00"
          }, {
            "count" : 7,
            "item" : "87324.00"
          }, {
            "count" : 6,
            "item" : "62796.00"
          }, {
            "count" : 5,
            "item" : "100048.00"
          }, {
            "count" : 4,
            "item" : "62916.00"
          }, {
            "count" : 3,
            "item" : "87372.00"
          }, {
            "count" : 2,
            "item" : "84760.00"
          }, {
            "count" : 1,
            "item" : "89772.80"
          } ]
        },
        "format" : {
          "precisionStyle" : "standard",
          "noCommas" : "false",
          "align" : "right"
        }
      } ],
      "grants" : [ {
        "inherited" : false,
        "type" : "viewer",
        "flags" : [ "public" ]
      } ],
      "metadata" : {
        "custom_fields" : {
          "Metadata" : {
            "Last Updated Date via Automated Load" : "",
            "Time Period" : "Last Updated April 19, 2012",
            "Data Owner" : "Human Resources",
            "Frequency" : "Data is updated quarterly"
          }
        },
        "renderTypeConfig" : {
          "visible" : {
            "table" : true
          }
        },
        "availableDisplayTypes" : [ "table", "fatrow", "page" ],
        "rdfSubject" : "0",
        "filterCondition" : {
          "value" : "AND",
          "children" : [ {
            "value" : "OR",
            "type" : "operator",
            "metadata" : {
              "includeAuto" : 15,
              "tableColumnId" : {
                "241512" : 1532236
              },
              "operator" : "EQUALS"
            }
          } ],
          "type" : "operator",
          "metadata" : {
            "unifiedVersion" : 2,
            "advanced" : true
          }
        },
        "rowIdentifier" : "0",
        "rdfClass" : ""
      },
      "owner" : {
        "id" : "vi9p-p863",
        "displayName" : "Eric Phillips",
        "emailUnsubscribed" : false,
        "privacyControl" : "login",
        "profileLastModified" : 1333412533,
        "roleName" : "administrator",
        "screenName" : "Eric Phillips",
        "rights" : [ "create_datasets", "edit_others_datasets", "edit_sdp", "edit_site_theme", "moderate_comments", "manage_users", "chown_datasets", "edit_nominations", "approve_nominations", "feature_items", "federations", "manage_stories", "manage_approval", "change_configurations", "view_domain", "view_others_datasets", "edit_pages", "create_pages" ]
      },
      "query" : {
      },
      "rights" : [ "read" ],
      "tableAuthor" : {
        "id" : "vi9p-p863",
        "displayName" : "Eric Phillips",
        "emailUnsubscribed" : false,
        "privacyControl" : "login",
        "profileLastModified" : 1333412533,
        "roleName" : "administrator",
        "screenName" : "Eric Phillips",
        "rights" : [ "create_datasets", "edit_others_datasets", "edit_sdp", "edit_site_theme", "moderate_comments", "manage_users", "chown_datasets", "edit_nominations", "approve_nominations", "feature_items", "federations", "manage_stories", "manage_approval", "change_configurations", "view_domain", "view_others_datasets", "edit_pages", "create_pages" ]
      },
      "tags" : [ "personnel" ],
      "flags" : [ "default" ]
    }
  },
  "data" : [ [ 8172, "56A29995-E979-411D-BE47-27F48FD1A0A1", 8172, 1334860745, "386464", 1334860745, "386464", "{\n}", "EMANUEL,  RAHM", "MAYOR", "MAYOR'S OFFICE", "216210.00" ]
 ]
}

今、私は「データ」リストの最後の行にある216210である特定の人の給与を取得しようとしています。

だからこれは私が今持っているものです:

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                return;
            }
            List<Employee> emp = JsonConvert.DeserializeObject<List<Employee>>(e.Result);
            this.lbTweets.ItemsSource = emp;
        }
    }

    public class Employee
    {


        [JsonProperty("data")]
        public string salary
        {
            get;
            set;
        }

    }

私は[JsonProperty("data")]正しい部分を持っていないと思います、そして私がボタンをクリックした後に私はこのエラーを受け取ります:

An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll

Additional information: Unexpected character encountered while parsing value: ?. Line 0, position 0.

誰か助けてもらえますか?ありがとう

編集:これが役立つかどうかはわかりませんが、次のコードを使用してjavascriptで解析しました:

$.getJSON(this.query, function(response) {
        var i, results;
        results = [];
        for (i = 0; i < response.data.length; i += 1) {
            row = { 
                name: response.data[i][8],
                salary: response.data[i][11]
            }
            results.push(row);
        }

更新:リンクをjsonpリクエストではなくjsonリクエストのみにした後、新しいjsonには余分な文字がなくなりました。しかし、新しいエラーが発生します。追加情報:JSONオブジェクトをタイプ'System.Collections.Generic.List`1[WPJsonSample.Tweet]'に逆シリアル化できません。行2、位置11。

json2csharpを使用してクラスを生成したので、次のクラスがあります。

public class RootObject
{
    public Meta meta { get; set; } // removed this line because I only want the data
    public List<List<object>> data { get; set; }
}

しかし、私はまだ上からそのエラーを受け取っています、それは私がこれのための正しいデータ型を持っていないということである可能性があります:

List<RootObject> tweets = JsonConvert.DeserializeObject<List<RootObject>>(e.Result);
4

1 に答える 1

1

本当にJSONPを使用する必要がありますか?http://en.wikipedia.org/wiki/JSONP

このURLは、単純なJSONオブジェクトを取得して逆シリアル化するのに適していますか? https://data.cityofchicago.org/api/views/xzkq-xp2w/rows.json?search=rahm

JSONPを使用する場合(例では「ajaxクロスドメインリクエスト」の場合)、最後にコールバック関数が必要です: https ://data.cityofchicago.org/api/views/xzkq-xp2w/rows.json?search=rahm&jsonp= callbackfunc (jqueryを使用したJSONPリクエストは、毎回一意のコールバック関数を追加します)。

于 2012-05-31T09:04:36.560 に答える