2012-05-31 59 views
2

我对json和c#有点新,我试图从api收集一些数据。部分链接到JSON数据是:https://data.cityofchicago.org/api/views/*REMOVED*/rows.json?search=rahm&jsonp=? 和给出的数据是有点乱:在Windows Phone中解析JSON c#

{ 
    "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); 
     } 

UPDATE:使链接只有一个JSON请求,而不是JSONP请求后,新json没有任何额外的字符了。但是我收到了一个新错误:其他信息:无法将JSON对象反序列化为类型'System.Collections.Generic.List`1 [WPJsonSample.Tweet]'。 11.

我已经使用json2csharp生成我的类2号线,位置,所以我有这个类:

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); 
+0

如果您在尝试调用DeserializeObject之前从JSON字符串的起始位置删除此字符,该错误会报告意外字符“?”,这是否有帮助? –

+0

这个问号包含在json uri中,不知道如何删除它。 – Dan

+0

你可以调用字符串“e.Result.Replace(”?“,string.empty)”替换“只是为了看看这是否有帮助? –

回答

1

你真的需要使用JSONP吗? http://en.wikipedia.org/wiki/JSONP

此网址能更好地获取和反序列化简单的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请求增加每次一个唯一的回调函数)。

+0

感谢您的支持,我正在使用它javascript早些时候,忘了删除这个,但我得到一个新的错误:其他信息:无法将JSON对象反序列化到类型'System.Collections.Generic.List'1 [Chicago.Tweet]'。第2行,位置11. – Dan

+0

是啊,这是关于反序列化,强类型对象和集合的常见问题。我让你做一些研究,因为我不知道你的对象的定义。有很多关于这方面的讨论(http://stackoverflow.com/questions/2546138/deserializing-json-data-to-c-sharp-using-json-net)。记住,反序列化一个列表,你需要一个列表(作为JSON数组)。可能这会帮助你更快地创建你的C#对象:http://json2csharp.com/ –