List assigned to Json deserialize is null - c#

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; }
}

Related

Map WebReponse to Model Class

I have a service that is posting data to a URL and listening for the response.
This is the service call
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = null;
response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, encode, true);
string strResponse = reader.ReadToEnd();
The string response that it's returning is
"{"TransactionConfirmationWrapedResult":"{\"ResultCode\":\"1\",\"ResultDesc\":\"Failed -\",\"ThirdPartyTransID\":\"\"}"}"
I would like to map this result to class like this;
public class M2PDto
{
[DataMember(Name = "TransactionConfirmationWrapedResult")]
public M2P2Dto WrapedResult { get; set; }
}
public class M2P2Dto
{
[DataMember(Name = "ResultCode")]
public string ResultCode { get; set; }
[DataMember(Name = "ResultDesc")]
public string ResultDesc { get; set; }
[DataMember(Name = "ThirdPartyTransID")]
public string ThirdPartyTransID { get; set; }
}
This is my deserializing code
M2PDto m2P = JsonConvert.DeserializeObject<M2PDto>(strResponse);
I can't seem to figure out how to deserialize it, keeps failing by passing null to m2P

c# deserialize mutliple Json into same list

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;
}
}

Deserialize XML into Object

Im trying to deserialize an xml response from from Amazons simple db service.
The xml is like this.
<ListDomainsResponse xmlns="http://sdb.amazonaws.com/doc/2009-04-15/">
<ListDomainsResult>
<DomainName>Logging</DomainName>
<DomainName>teets</DomainName>
</ListDomainsResult>
<ResponseMetadata>
<RequestId>9d48122e-1ddf-8771-b771-b36599838ea0</RequestId>
<BoxUsage>0.0000071759</BoxUsage>
</ResponseMetadata>
</ListDomainsResponse>
And the object i'm trying to serialize into is this.
public class ListDomainsResponse : Response
{
public ListDomainsResult ListDomainsResult { get; set; }
}
public class ListDomainsResult
{
public List<String> DomainNames { get; set; }
public string NextToken { get; set; }
}
public class Response
{
public static string NameSpace = "http://sdb.amazonaws.com/doc/2009-04-15/";
public ResponseMetadata ResponseMetadata{ get; set; }
}
public class ResponseMetadata
{
public string RequestId { get ; set; }
public string BoxUsage { get; set; }
}
My problem is that the response metadata is getting deserialized correctly but the List of strings called DomainName is not being deserialized, it is empty each time.
Can someone see what i'm doing wrong.
Updated with more code
public async Task<Response> ListDomains (ListDomainsRequest request)
{
using (Client = new HttpClient ()) {
ListDomainsRequestMarshaller marshaller = new ListDomainsRequestMarshaller ();
marshaller.Configure (request);
HttpResponseMessage responseMessage = Client.GetAsync (marshaller.Marshal ()).Result;
ListDomainsResponseUnMarshaller unmarshaler = new ListDomainsResponseUnMarshaller (responseMessage);
return unmarshaler.Response;
}
}
public ListDomainsResponseUnMarshaller (HttpResponseMessage message)
{
XDocument doc = XDocument.Load (message.Content.ReadAsStreamAsync ().Result);
if (message.StatusCode.Equals (HttpStatusCode.OK)) {
Serializer = new XmlSerializer (typeof(ListDomainsResponse), Response.NameSpace);
Response = (ListDomainsResponse)Serializer.Deserialize (doc.CreateReader ());
Response.HttpStatusCode = message.StatusCode;
Response.ContentLength = (long)message.Content.Headers.ContentLength;
} else {
Serializer = new XmlSerializer (typeof(Response));
Response = (Response)Serializer.Deserialize (doc.CreateReader ());
Response.HttpStatusCode = message.StatusCode;
Response.ContentLength = (long)message.Content.Headers.ContentLength;
}
Serializer = null;
}
Adding Attributes helped
[System.Xml.Serialization.XmlTypeAttribute (Namespace = "http://sdb.amazonaws.com/doc/2009-04-15/")]
[System.Xml.Serialization.XmlRootAttribute ("ListDomainsResponse", Namespace = "http://sdb.amazonaws.com/doc/2009-04-15/")]
public class ListDomainsResponse : Response
{
public ListDomainsResult ListDomainsResult { get; set; }
}
[System.Xml.Serialization.XmlTypeAttribute (Namespace = "http://sdb.amazonaws.com/doc/2009-04-15/")]
[System.Xml.Serialization.XmlRootAttribute (Namespace = "http://sdb.amazonaws.com/doc/2009-04-15/")]
public class ListDomainsResult
{
[System.Xml.Serialization.XmlElementAttribute ("DomainName")]
public string[] DomainName { get; set; }
public string NextToken { get; set; }
}

JSON deserialization not working after changing response string

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

JsonConvert.PopulateObject - Returns empty object

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

Categories

Resources