Parsing JSON from API - C# - c#

I am requesting a JSON from a standard web service and I need to handle the response so I can work with the objects. I am working in Xamarin Studio - not that i think that matters.
You can see a result from web service by:
https://dawa.aws.dk/vejnavne/autocomplete?q=due
This is requesting street names in Denmark with 'due' in it.
public async Task doAsyncAddress(string input)
{
var template = "https://dawa.aws.dk/vejnavne/autocomplete?q={0}";
var url = string.Format(template, input);
using (var httpClient = new HttpClient())
{
try
{
Task<HttpResponseMessage> getResponse = httpClient.GetAsync(url);
HttpResponseMessage response = await getResponse;
var responseJsonString = await response.Content.ReadAsStringAsync();
/*
I have tried different things here, with with JsonConvert and JObject but neither works.. I have an idea that the son string is in wrong format, with "\n" included and i have tried to remove these, but still without results. I can see the string so I know it is there.. But it is not formatted correctly.
*/
}
catch (Exception ex)
{
string message = ex.Message;
return message;
}
}
}
With the JsonConverter.DeserializeObject i do this:
var adress = JsonConvert.DeserializeObject<List<Address>>(responseJsonString);
where Address:
public class Address
{
public string tekst { get; set; }
public List<Vejnavne> vejnavn
{ get; set; }
}
public class Vejnavne
{
public string href { get; set; }
public string navn { get; set; }
}
and the response is:
"Cannot deserialize the current JSON object (e.g.
{\"name\":\"value\"}) into type
'System.Collections.Generic.List`1[MinEjendom.Vejnavne]' because the
type requires a JSON array (e.g. [1,2,3]) to deserialize
correctly.\nTo fix this error either change the JSON to a JSON array
(e.g. [1,2,3]) or change the deserialized type so that it is a normal
.NET type (e.g. not a primitive type like integer, not a collection
type like an array or List) that can be deserialized from a JSON
object. JsonObjectAttribute can also be added to the type to force it
to deserialize frenter code hereom a JSON object.\nPath
'[0].vejnavn.href', line 5, position 11.”
And with JObject i get:
"Error reading JObject from JsonReader. Current JsonReader item is not
an object: StartArray. Path '', line 1, position 1."

Your C# code is wrong. This is the correct one:
public class Vejnavn
{
public string href { get; set; }
public string navn { get; set; } // not List<Vejnavne> vejnavn
}
public class Address
{
public string tekst { get; set; }
public Vejnavn vejnavn { get; set; }
}
Then call it like this:
var adress = JsonConvert.DeserializeObject<List<Address>>(responseJsonString);

When you've JSON, you are .NET developer and finally - you have to convert JSON to C# class, you should use Edit - > Paste Special -> Paste JSON as classes. This is an awesome tool :)
Your code is wrong. This is the generated class from your JSON :
public class Class1
{
public string tekst { get; set; }
public Vejnavn vejnavn { get; set; }
}
public class Vejnavn
{
public string href { get; set; }
public string navn { get; set; }
}
When you have successfully generated your code, you can rename the class.

Related

Deserialize JSON property into different class properties

I am receiving API responses from 3rd party that have ambiguous types. For some methods it is:
{"error":{"message":"Resource is already part of this app","status_code":400}}
And on other calls it is:
{"error": "Resource is already part of this app" }
Is it possible to deserialize such responses into something like:
public class Response
{
[JsonProperty("error")]
public string Error { get; set; }
[JsonIgnore] //[JsonProperty("error")]
public ObjectError ObjectError { get; set; }
}
public class ObjectError
{
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("status_code")]
public string StatusCode { get; set; }
}
UPDATE
So I have ended up using object as catch all for deserialization.
[JsonProperty("error")]
public object Error { get; set; }
public string ErrorAsString => Error is string ? Error.ToString() : null;
public ObjectError ErrorAsObject => Error is string ? null : Error != null ? JsonConvert.DeserializeObject<ObjectError>(Error.ToString()) : null;
It's not ideal, I know.
You can do this easily using NetwosoftJson. Here you can check on how to deserialize into an object using it.
You could always parse the JSON object and check to see if it has specific fields. For example:
JObject json = JObject.Parse(jsonString);
if(json.HasKey("message"))
{
var result = JsonConvert.DeserializeObject<ObjectError>(jsonString);
// Do some stuff here
}
else
{
var result = JsonConvert.DeserializeObject<Response>(jsonString);
// Do some stuff here
}

How to deserialize json string NOT to List<object> in c#

i'm working with Vk.com api, in particular with this json string:
{
"response":
[338775,
{"aid":108787020,
"owner_id":2373452,
"artist":" Moby",
"title":"Flowers",
"duration":208,
"url":"https:\/\/cs1-50v4.vk-cdn.net\/p3\/c762273870cc49.mp3?extra=t9I-RMkSlAHkhe8JtOUUZBTZqkFVE9MJ_Q-TPmOhxPHTfHazQWEYBf4LqrOY64xLX9AuzaKwvLo4PECSFiHyWM53WMDWVcBAZVT5jlIbZ9X8ag","lyrics_id":"6060508",
"genre":22}
]
}
I have a class for parsing data:
public class AlbumResponse
{
[JsonProperty("artist")]
public string artist { get; set; }
[JsonProperty("title")]
public string title { get; set; }
[JsonProperty("duration")]
public string duration { get; set; }
[JsonProperty("url")]
public string url { get; set; }
}
And List for deserialization:
public class VkAlbum
{
public List<AlbumResponse> response { get; set; }
}
Than I use
var album = JsonConvert.DeserializeObject<VkAlbum>(responseText);
BUT it doesn't work (A first chance exception of type 'Newtonsoft.Json.JsonSerializationException') because of "338775" after "response".
So how can I deserialize it without using
public List<object> response { get; set; }
instead of my AlbumResponse class?
A primitive JSON sanitation for your disposal. Not the most elegant code, but i'm sure you can take it from here.
responseText = Regex.Replace(responseText, "[\t|\r\n]", "");
if (responseText.IndexOf("response\": [") != -1)
{
int start = responseText.IndexOf('[') + 1;
int end = responseText.IndexOf(',', start);
responseText = responseText.Substring(0, start) + responseText.Substring(end + 1);
}
var album = JsonConvert.DeserializeObject<VkAlbum>(responseText);
technically json response is not equivalent to C# List<AlbumResponse>. JSON array allows mixed types so essentially it can contains numbers and other nested objects, in your case AlbumResponse.
you can avoid exception by using List<object> and checking it's first element if it's number, if it is, ignore or do whatever you want to do and typecast 2nd element in list to AlbumResponse.
e.g.
var res = response [1] as AlbumResponse;
if(res!=null)
{
// do something interesting...

Converting JSON to List

I am stuck in a step that I am sure should work. I have a method (in a separate class) that should return a List as its value after processing the JSON. I am going to paste the code skipping the JSON configuration stuff:
public static dynamic CustInformation(string Identifier)
{
//SKIPPED JSON CONFIG STUFF (IT'S WORKING CORRECTLY)
var result = "";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
dynamic d;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
return JsonConvert.DeserializeObject<List<Models.RootObject>>(result);
}
The model was generated using C# to Json converter:
public class Record
{
public string idIdentifier { get; set; }
public string KnowName1 { get; set; }
public string KnowAddress1 { get; set; }
public string KnowRelation1 { get; set; }
public string KnowPhone1 { get; set; }
public string KnowName2 { get; set; }
public string KnowAddress2 { get; set; }
//.....skipped other variables
}
public class RootObject
{
public List<Record> record { get; set; }
}
And I am calling the method like this:
var model = Classes.EndPoint.CustInformation(identifier);
Yet I am getting this error everytime:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Models.RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change
the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'record', line 1, position 10.
EDIT: JSON
{
"record": [
{
Identifier": "DQRJO1Q0IQRS",
"KnowName1": "",
"KnowAddress1": "",
"KnowRelation1": "",
"KnowPhone1": "",
"KnowName2": "",
"KnowAddress2": "",
//.....MORE STYFF
}
]
}
Like I said in the comments, and like the error message clearly states, you're trying to deserialize into a list of root objects, but your JSON is only one root object, not an array.
Here's what your C# should be.
return JsonConvert.DeserializeObject<Models.RootObject>(result);

deserialize json into .net object using json.net

I am having a problem deserializing some JSON string back into .net objects. I have a container class which contains some information from external and there is a field call ClassType which defined what type of information is that and the actual content is in another property, which currently can be anything, so we define that as an Object type.
Following are the .net class definition which helps to understand the issue.
class ClassOne
{
public string Name { get; set; }
public int Age { get; set; }
}
class ClassTwo
{
public string AddressLine { get; set; }
public string AddressLine2 { get; set; }
}
class ClassThree
{
public string Country { get; set; }
public string Passport { get; set; }
}
class ContainerClass
{
public string ClassType { get; set; }
public object ClassContent { get; set; }
}
When getting the information from external in a JSON format it will be something like:
{"ClassType":"Class1","ClassContent":{"Name":"James","Age":2}}
I am using Newtonsoft JSON.net library to deserialize the JSON string. It seems like that the default deserialize function will just deserialize that into an Newtonsoft.Json.Linq.JContainer. I just wondering how can I write some Converter to deserialize the ClassContent based on the ClassType definition. Any code sample will be highly appreciated.
I would go dynamic way, like:
string json = #"{""ClassType"":""Class1"",""ClassContent"":{""Name"":""James"",""Age"":2}}";
dynamic jObj = JObject.Parse(json);
if (jObj.ClassType == "Class1")
{
Console.WriteLine("{0} {1}", jObj.ClassContent.Name, jObj.ClassContent.Age);
}
Since returning an object (ClassContent) doesn't mean much, and you have to cast it to a concrete class somehow (using some if's or switch).
Sample:
var container = JsonConvert.DeserializeObject<ContainerClass>(json);
JContainer content = (JContainer)container.ClassContent;
switch(container.ClassType)
{
case "Class1": return container.ToObject(typeof(ClassOne));
..
}
use dynamic and call .ToObject(Type type)
dynamic root = JObject.Parse(json)
return root["ClassContent"].ToObject(Type.GetType(root["ClassType"]))
Try the following
var jsonObject = JObject.Parse(jsonString);
var result = jsonObject.ToObject(Type.GetType("namespace.className"));

Metro App - deserialize JSON String

I'd like to deserialize a JSON string which I get from a webservice. My problem is, that the deserialized object class array (of type Result) has always 0 items in it....
But the webservice returns the correct string.
So I think the failure occurs in the way how I deserialize the string/stream.
Any ideas what's my fault?
//JSON result string:
{"Results":
[{"Result":{
"Name":"Rechnung2",
"Date1":"2012-10-05",
"Item1":"50",
"Item2":"10",
"CompanyName":"Contoso",
"Description":"My description"}}]
}
[DataContract]
public class Result
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Date1 { get; set; }
[DataMember]
public string Item1 { get; set; }
[DataMember]
public string Item2 { get; set; }
[DataMember]
public string CompanyName { get; set; }
[DataMember]
public string Description { get; set; }
}
public async void GetjsonStream()
{
HttpClient client = new HttpClient();
string url = "http://localhost/test/api.php?format=json&key=12345";
HttpResponseMessage response = await client.GetAsync(url);
//ReadAsStringAsync() works fine, so I think ReadAsStreamAsync() works also fine
var str = await response.Content.ReadAsStreamAsync();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Result[]));
//Result has always 0 items
Result[] res = (Result[])ser.ReadObject(str);
}
I haven't used DataContractJsonSerializer myself, so this may not be the best approach - but I suspect that the problem is that the JSON represents "an object containing a collection of results" - not "a collection of results".
Try this, in addition to your existing code:
[DataContract]
public class ResultCollection
{
[DataMember]
public Result[] Results { get; set; }
}
...
var ser = new DataContractJsonSerializer(typeof(ResultCollection));
var collection = (ResultCollection)ser.ReadObject(str);
var results = collection.Results;
You may be able to change the type of Results to List<Result> too, if that's helpful.
(I've just tried the code above, and it gave me the right result, so it looks like this is at least along the right lines...)

Categories