Я использую WebRequest для чтения данных JSON из FCC, чтобы я мог вывести их в представление.Вот мой пользовательский класс для хранения лицензии FCC:
public class License
{
public string Name{ get; set; }
public string Frn { get; set; }
public string Callsign { get; set;}
public string CategoryDesc { get; set; }
public string ServiceDesc { get; set; }
public string StatusDesc { get; set; }
public DateTime ExpiredDate { get; set; }
public string Id { get; set; }
public string DetailUrl { get; set; }
}
Вот действие Controller, которое я использую для чтения результатов json.
У меня Verizon Wireless жестко задан в качестве значения поиска длясейчас:
public ActionResult GetLicenses()
{
var result = string.Empty;
var url = "http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=Verizon+Wireless&format=jsonp&jsonCallback=?";
var webRequest = WebRequest.Create(url);
webRequest.Timeout = 2000;
using (var response = webRequest.GetResponse() as HttpWebResponse)
{
if (response.StatusCode == HttpStatusCode.OK)
{
var receiveStream = response.GetResponseStream();
if (receiveStream != null)
{
var stream = new StreamReader(receiveStream);
result = stream.ReadToEnd();
}
}
}
return new ContentResult { Content = result, ContentType = "application/json" };
}
Вот вид.Я пытаюсь перечислить все лицензии и вывести их в таблицу, но когда я захожу в / Home / GetLicenses, мне предлагается загрузить файл:
@model IEnumerable<MvcApplication1.Models.License>
@{
ViewBag.Title = "Licenses";
}
<h2>Licenses</h2>
<table>
<tr>
<th>
Name
</th>
<th>
Frn
</th>
<th>
Callsign
</th>
<th>
CategoryDesc
</th>
<th>
ServiceDesc
</th>
<th>
StatusDesc
</th>
<th>
ExpiredDate
</th>
<th>
DetailUrl
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Frn)
</td>
<td>
@Html.DisplayFor(modelItem => item.Callsign)
</td>
<td>
@Html.DisplayFor(modelItem => item.CategoryDesc)
</td>
<td>
@Html.DisplayFor(modelItem => item.ServiceDesc)
</td>
<td>
@Html.DisplayFor(modelItem => item.StatusDesc)
</td>
<td>
@Html.DisplayFor(modelItem => item.ExpiredDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.DetailUrl)
</td>
</tr>
}
</table>
Я получу вышеуказанное, работающее, если ясделать это напрямую через метод getJSON jquery, но я хотел посмотреть, смогу ли я получить результаты из контроллера в представление и затем отобразить его в представлении.
Это пример того, что возвращается впеременная результатов:
?({
"status": "OK",
"Licenses": {
"page": "1",
"rowPerPage": "100",
"totalRows": "1995",
"lastUpdate": "Sep 21, 2011",
"License": [
{
"licName": "CELLCO PARTNERSHIP (\"VERIZON WIRELESS\")",
"frn": "",
"callsign": "",
"categoryDesc": "Satellite Earth Station",
"serviceDesc": "",
"statusDesc": "Active",
"expiredDate": "",
"licenseID": "2300007967",
"licDetailURL": "http://licensing.fcc.gov/cgi-bin/ws.exe/prod/ib/forms/reports/swr031b.hts?prepare=&column=V_SITE_ANTENNA_FREQ.file_numberC/File+Number&q_set=V_SITE_ANTENNA_FREQ.file_numberC/File+Number/=/FCNNEW2000060800036"
},
{
"licName": "CELLO PARTNERSHIP (\"VERIZON WIRELESS\")",
"frn": "",
"callsign": "",
"categoryDesc": "Satellite Earth Station",
"serviceDesc": "",
"statusDesc": "Active",
"expiredDate": "",
"licenseID": "2300010661",
"licDetailURL": "http://licensing.fcc.gov/cgi-bin/ws.exe/prod/ib/forms/reports/swr031b.hts?prepare=&column=V_SITE_ANTENNA_FREQ.file_numberC/File+Number&q_set=V_SITE_ANTENNA_FREQ.file_numberC/File+Number/=/FCNNEW2000083100048"
},
{
"licName": "Cellco Partnership d/b/a Verizon Wireless",
"frn": "0003290673",
"callsign": "KE2XMC",
"categoryDesc": "Experimental",
"serviceDesc": "Experimental Developmental",
"statusDesc": "Unknown",
"expiredDate": "12/14/2000",
"licenseID": "3000020853",
"licDetailURL": "https://fjallfoss.fcc.gov/oetcf/els/reports/ELSSearchResult.cfm?callsign=KE2XMC"
},
{
"licName": "Cellco Partnership d/b/a Verizon Wireless",
"frn": "0003290673",
"callsign": "WA2XPS",
"categoryDesc": "Experimental",
"serviceDesc": "Experimental Developmental",
"statusDesc": "Unknown",
"expiredDate": "12/14/2000",
"licenseID": "3000020851",
"licDetailURL": "https://fjallfoss.fcc.gov/oetcf/els/reports/ELSSearchResult.cfm?callsign=WA2XPS"
},
{
"licName": "Cellco Partnership dba Verizon Wireless",
"frn": "0003290673",
"callsign": "KNKP866",
"categoryDesc": "Mobile/Fixed Broadband",
"serviceDesc": "Cellular",
"statusDesc": "Cancelled",
"expiredDate": "10/01/2005",
"licenseID": "13328",
"licDetailURL": "http://wireless2.fcc.gov/UlsApp/UlsSearch/license.jsp?__newWindow=false&licKey=13328"
}
]
}
})
Я добавил этот класс:
public class FCC
{
public string status { get; set; }
public Licenses Licenses { get; set; }
}
Но я все еще получаю примитив JSON Invalid.
public ActionResult GetLicenses()
{
var result = string.Empty;
var url =
"http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=Verizon+Wireless&format=jsonp&jsonCallback=?";
var webRequest = WebRequest.Create(url);
webRequest.Timeout = 2000;
webRequest.ContentType = "application/json";
using (var response = webRequest.GetResponse() as HttpWebResponse)
{
if (response.StatusCode == HttpStatusCode.OK)
{
var receiveStream = response.GetResponseStream();
if (receiveStream != null)
{
var stream = new StreamReader(receiveStream);
result = stream.ReadToEnd();
}
}
}
FCC fcc = new FCC();
if (result.StartsWith(@"?("))
{
result = result.Substring(2);
}
if (result.EndsWith(@")"))
{
result = result.Remove(result.Length - 1);
}
if (result != null)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
fcc = serializer.Deserialize<FCC>(result);
}
return View(fcc.Licenses.License);
}