I want to deserialize following string:
YAHOO.Finance.SymbolSuggest.ssCallback({"ResultSet":{"Query":"google","Result":[{"symbol":"GOOG","name": "Google Inc.","exch": "NMS","type": "S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"GGQ1.DE","name": "GOOGLE-A","exch": "GER","type": "S","exchDisp":"XETRA","typeDisp":"Equity"}]}})
Which I download with a WebClient. But before I can deserialize, I need to remove "YAHOO.Finance.SymbolSuggest.ssCallback(" and the ")" at the end:
// get response
WebResponse response = request.EndGetResponse(result);
Stream stream = response.GetResponseStream();
// convert to string
StreamReader reader = new StreamReader(stream);
string output = reader.ReadToEnd();
// cut string
output = output.Substring(39, output.Length - 40);
// convert back to stream
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(output));
So, the response I get is a stream. I convert that stream to a string, change the string and then convert the string back to a stream. Next, I try to deserialize:
// parse json
System.Runtime.Serialization.Json.DataContractJsonSerializer rootSer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(RootObject));
// get root object
RootObject root = (RootObject)rootSer.ReadObject(ms);
// add to listbox
foreach (Result res in root.ResultSet.Result)
{
Dispatcher.BeginInvoke(() => ResultList.Add(res));
}
The problem is I get a nullpointexeption in the foreach...
My classes are like so:
public class Result
{
public string symbol { get; set; }
public string name { get; set; }
public string exch { get; set; }
public string type { get; set; }
public string exchDisp { get; set; }
public string typeDisp { get; set; }
}
public class ResultSet
{
public string Query { get; set; }
public List<Result> Result { get; set; }
}
public class RootObject
{
public ResultSet ResultSet { get; set; }
}
You can use Regex to extract the json from jsonp
output = Regex.Match(output, #".+?\((.+?)\)").Groups[1].Value
Related
I want to get all variables from https://api.coinmarketcap.com/v1/ticker/ in my c# console application.
How can I do this?
I started with getting the whole page as a stream. What to do now?
private static void start_get()
{
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create
(string.Format("https://api.coinmarketcap.com/v1/ticker/"));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
Console.WriteLine(_Answer.ReadToEnd());
}
First you need a custom class to use for deserialization:
public class Item
{
public string id { get; set; }
public string name { get; set; }
public string symbol { get; set; }
public string rank { get; set; }
public string price_usd { get; set; }
[JsonProperty(PropertyName = "24h_volume_usd")] //since in c# variable names cannot begin with a number, you will need to use an alternate name to deserialize
public string volume_usd_24h { get; set; }
public string market_cap_usd { get; set; }
public string available_supply { get; set; }
public string total_supply { get; set; }
public string percent_change_1h { get; set; }
public string percent_change_24h { get; set; }
public string percent_change_7d { get; set; }
public string last_updated { get; set; }
}
Next, you can use Newtonsoft Json, a free JSON serialization and deserialization framework in the following way to get your items (include the following using statements):
using System.Net;
using System.IO;
using Newtonsoft.Json;
private static void start_get()
{
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("https://api.coinmarketcap.com/v1/ticker/"));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
string jsonString;
using (Stream stream = WebResp.GetResponseStream()) //modified from your code since the using statement disposes the stream automatically when done
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
jsonString = reader.ReadToEnd();
}
List<Item> items = JsonConvert.DeserializeObject<List<Item>>(jsonString);
Console.WriteLine(items.Count); //returns 921, the number of items on that page
}
Finally, the list of elements is stored in items.
A simplified version of Keyur PATEL's work.
static void GetCoinValues()
{
string json = new WebClient().DownloadString("https://api.coinmarketcap.com/v1/ticker/");
List<Item> items = JsonConvert.DeserializeObject<List<Item>>(json);
foreach (var item in items)
{
Console.WriteLine("ID: " + item.id.ToUpper());
Console.WriteLine("Name: " + item.name.ToUpper());
Console.WriteLine("Symbol: " + item.symbol.ToUpper());
Console.WriteLine("Rank: " + item.rank.ToUpper());
Console.WriteLine("Price (USD): " + item.price_usd.ToUpper());
Console.WriteLine("\n");
}
}
I can't make it work. Here's the JSON:
https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas/59/modelos
And here's my code:
using (var client = new WebClient())
{
var url = string.Format("https://fipe-parallelum.rhcloud.com/api/v1/{0}/marcas/{1}/modelos", tipo_cb.SelectedValue, marca_cb.SelectedValue);
//Download do resultado
var json = client.DownloadString(url);
List<Modelos> plist = JsonConvert.DeserializeObject<List<Modelos>>(json);
foreach (Modelos data in plist)
{
modelo_cb.Items.Add(new ListItem(data.nome.ToString(), data.codigo.ToString()));
}
}
Class
public class Modelos
{
public string nome;
public string codigo;
}
Thanks!
The problem is that your data model does not match your JSON. Your root JSON container is an object (a collection of name/value pairs, surrounded by braces), not an array: {"modelos":[...]}. Such an object needs to be mapped to a non-collection c# object, as is explained in the Json.NET documentation. And in fact if you simply post your JSON link to http://json2csharp.com/ it will design a correct data model for you:
public class Modelo
{
public string nome { get; set; }
public int codigo { get; set; }
}
public class Ano
{
public string nome { get; set; }
public string codigo { get; set; }
}
public class RootObject
{
public List<Modelo> modelos { get; set; }
public List<Ano> anos { get; set; }
}
Then deserialize as follows:
List<Modelo> plist;
using (var client = new WebClient())
{
var url = #"https://fipe-parallelum.rhcloud.com/api/v1/carros/marcas/59/modelos";
//Download do resultado
var json = client.DownloadString(url);
var root = JsonConvert.DeserializeObject<RootObject>(json);
plist = root.modelos;
}
Example fiddle.
Since my last question was marked as duplicate and i didn't find an answer in recommended link i'm asking my question again.
I want to deserialize some JSON data and pass it to view in my ASP.NET MVC 5 app.
My model:
public class OrderModel
{
public string Seat { get; set; }
public string objectId { get; set; }
public DateTime? createdAt { get; set; }
}
I've read that to deserialize JSON data to list of object i have to make "root" class which is here:
public class OrderRootModel
{
public List<OrderModel> OrderList { get; set; }
}
Here is a method that gets JSON data in ApiModel class and assignsit to list. This method is probably a problem since it is returning null :
//ApiModel class
public OrderRootModel GetOrderData()
{
string url = "https://api.parse.com/1/classes/Orders";
OrderRootModel model = new OrderRootModel();
model = JsonConvert.DeserializeObject<OrderRootModel>(getParseIdData(url));
return model;
}
Here is a method that gets JSON string:
public string getParseIdData(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("X-Parse-Application-Id", id);
request.Headers.Add("X-Parse-REST-API-KEY", key);
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
return reader.ReadToEnd();
}
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
Console.WriteLine(errorText);
}
throw;
}
}
Here is getParseIdData() response:
{"results":[{"Seat":"5","createdAt":"2015-11-29T18:50:15.320Z","objectId":"BsDSolYPsT","updatedAt":"2015-11-29T19:40:55.020Z"},{"Seat":"6","createdAt":"2015-12-02T22:31:36.892Z","objectId":"kQJ0R5TUvw","updatedAt":"2015-12-02T22:31:36.892Z"},{"Seat":"7","createdAt":"2015-12-02T22:31:40.261Z","objectId":"sVtdj3aipb","updatedAt":"2015-12-02T22:31:40.261Z"},{"Seat":"8","createdAt":"2015-12-02T22:31:43.082Z","objectId":"7oH2ySrDFH","updatedAt":"2015-12-02T22:31:43.082Z"}]}
My controller:
public ActionResult Index()
{
ApiModel model = new ApiModel();
return View(model.GetOrderData()) ;
}
So probably i've made a mistake somewhere in GetOrderData() method since it returns null. Can you help me to solve this issue?
Based on your code this is what would the C# Class should look like when converting your JSON to a C# Class
{
"results":[
{"Seat":"5","createdAt":"2015-11-29T18:50:15.320Z","objectId":"BsDSolYPsT","updatedAt":"2015-11-29T19:40:55.020Z"},
{"Seat":"6","createdAt":"2015-12-02T22:31:36.892Z","objectId":"kQJ0R5TUvw","updatedAt":"2015-12-02T22:31:36.892Z"},
{"Seat":"7","createdAt":"2015-12-02T22:31:40.261Z","objectId":"sVtdj3aipb","updatedAt":"2015-12-02T22:31:40.261Z"},
{"Seat":"8","createdAt":"2015-12-02T22:31:43.082Z","objectId":"7oH2ySrDFH","updatedAt":"2015-12-02T22:31:43.082Z"}
]
}
public class Result
{
public string Seat { get; set; }
public string createdAt { get; set; }
public string objectId { get; set; }
public string updatedAt { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
}
I have a Api link that returns this Json structure. In the code I request this Api link and then deserialize it into a list. This is done without problems. But if the Api returns more than 50 "counts" it creates another page. How do i get around to loop through all pages and add everything to the existing list?
In the case i linked there will be 38 pages. All need to be added to the list.
Call
// spidyApiUrl = http://www.gw2spidy.com/api/v0.9/json/item-search/iron/1
var spidyApi_idByName_result = api_Handler.objFromApi_idToName(spidyApiUrl);
Function with the return
public RootObject objFromApi_idToName(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
var jsonReader = new JsonTextReader(reader);
var serializer = new JsonSerializer();
//return serializer.Deserialize<RootObject>(jsonReader);
RootObject rootObject = serializer.Deserialize<RootObject>(jsonReader);
if (rootObject.count > 0) { return rootObject; }
else { return null; }
}
}
And ofcourse i also have the get; set; classes.
How do I loop through all pages (if mutliple pages exist, which doesnt have to) and add these to the same object list.
You need continue downloading the data until page == last_page
As you get each page of data you then add the new set of results to the original rootObject's results property with AddRange
I also changed the url that gets passed into the function from
http://www.gw2spidy.com/api/v0.9/json/item-search/iron/1
to
http://www.gw2spidy.com/api/v0.9/json/item-search/iron
This allows me to add the page numbers to the url to get all the pages
http://www.gw2spidy.com/api/v0.9/json/item-search/iron/1
http://www.gw2spidy.com/api/v0.9/json/item-search/iron/2
.....
http://www.gw2spidy.com/api/v0.9/json/item-search/iron/38
Code:
public class Result
{
public int data_id { get; set; }
public string name { get; set; }
public int rarity { get; set; }
public int restriction_level { get; set; }
public string img { get; set; }
public int type_id { get; set; }
public int sub_type_id { get; set; }
public string price_last_changed { get; set; }
public int max_offer_unit_price { get; set; }
public int min_sale_unit_price { get; set; }
public int offer_availability { get; set; }
public int sale_availability { get; set; }
public int sale_price_change_last_hour { get; set; }
public int offer_price_change_last_hour { get; set; }
}
public class RootObject
{
public int count { get; set; }
public int page { get; set; }
public int last_page { get; set; }
public int total { get; set; }
public List<Result> results { get; set; }
}
class Program
{
static void Main(string[] args)
{
objFromApi_idToName("http://www.gw2spidy.com/api/v0.9/json/item-search/iron");
}
public static RootObject objFromApi_idToName(string url)
{
RootObject rootObject = null;
RootObject tempRootObject = null;
int page = 1;
do
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "/" + page);
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
var jsonReader = new JsonTextReader(reader);
var serializer = new JsonSerializer();
//return serializer.Deserialize<RootObject>(jsonReader);
tempRootObject = serializer.Deserialize<RootObject>(jsonReader);
if (rootObject == null)
{
rootObject = tempRootObject;
}
else
{
rootObject.results.AddRange(tempRootObject.results);
rootObject.count += tempRootObject.count;
}
}
page++;
} while (tempRootObject != null && tempRootObject.last_page != tempRootObject.page);
return rootObject;
}
}
Are you using Web API? If so, could you try something like this?
public RootObject objFromApi_idToName(string url)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("<your uri here>");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("<uri extention here>");
if (response.IsSuccessStatusCode)
{
string jsonStr = await response.Content.ReadAsStringAsync();
var deserializedResponse = JsonConvert.DeserializeObject<List<your model class here>>(jsonStr);
return deserializedResponse;
}
}
The following bit of code attempts to read some json and populate an object:
public Response ParseObject(string Json)
{
Response response = new Response();
JsonConvert.PopulateObject(Json, response);
return response;
}
Here's the Response object:
public class Response
{
public string id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string status { get; set; }
public string incorporationDate { get; set; }
public string latestAnnualReturnDate { get; set; }
public string latestAccountsDate { get; set; }
public string companyType { get; set; }
public string accountsType { get; set; }
Unfortunately, the object (response) is empty ie (response.id is null as are all of the other properties).
I'm guessing that I need to pass in some JsonSerializerSettings but I can't find a tutorial anywhere?
You might want to check your Json string. When I run your code with the below set-up I get the values in the Response object.
var s = "{ \"id\":\"2\" , \"name\":\"Doe\" }";
Response response = ParseObject(s);
Response Try to use the above. This is to get the string from HttpContext
Stream dataStream = context.Request.InputStream;
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
Response data =(Response )json_serializer.DeserializeObject(responseFromServer);
In your case you can use
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
Response data =(Response )json_serializer.DeserializeObject(Json);