Converting JSON to C# class - c#

I have this JSON string:
[{"fkp_keyword":"CLI_RID"},
{"fkp_keyword":"DOC_NAME"},
{"fkp_keyword":"FILENAME"},
{"fkp_keyword":"PRINT_DATE"},
{"fkp_keyword":"EVENT_CODE"},
{"fkp_keyword":"CONFL_RID"},
{"fkp_keyword":"PROGRAM_CODE"},
{"fkp_keyword":"CES"},
{"fkp_keyword":"DISTR"},
{"fkp_keyword":"REC_DATE"},
{"fkp_keyword":"REC_RID"},
{"fkp_keyword":"PFL_RID"},
{"fkp_keyword":"DES"},
{"fkp_keyword":"CER_RID"}
]
I need to convert it into a List of the class kw below.
Definitions:
public class kw
{
public string fkp_keyword { get; set; }
}
But this code:
List<kw> header = new List<kw>();
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string result = client.DownloadString(parms);
header = JsonConvert.DeserializeObject<List<kw>>(result);
}
The call returns the JSON string above but when trying to convert it, the code above returns this exception:
Error converting value to type 'System.Collections.Generic.List[LA.Models.kw]
Update
I changed the definitions to this:
public class kwList
{
public kw[] Property1 { get; set; }
}
public class kw
{
public string fkp_keyword { get; set; }
}
and the code to this:
kwList header = new kwList();
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string result = client.DownloadString(parms);
header = JsonConvert.DeserializeObject<kwList>(result);
}
But now I'm getting this Exception:
Could not cast or convert from System.String to LicenseeArchive.Models.kwList.
What am I doing wrong?

For whatever reason, it appears that the JSON string returned by that URL is double-serialized. That is, it contains extra backslashes to escape all the quotes, which then prevents it from being deserialized properly to an array of objects. That is why you are getting an error.
To work around the problem, you can deserialize it twice: first to unescape the JSON, the second to do the "real" deserialization to your classes. Longer term, you may also wish to contact the provider of the API to see if they will fix their JSON.
List<kw> header = new List<kw>();
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string result = client.DownloadString(parms);
string unescapedJson = JsonConvert.DeserializeObject<string>(result);
header = JsonConvert.DeserializeObject<List<kw>>(unescapedJson);
}
Fiddle: https://dotnetfiddle.net/XEULdy

JSON string you provided can be loaded with your first class definition:
public class kw
{
public string fkp_keyword { get; set; }
}
Example:
string example = "[{\"fkp_keyword\":\"CLI_RID\"}, {\"fkp_keyword\":\"DOC_NAME\"}, {\"fkp_keyword\":\"FILENAME\"}]";
List<kw> kws = JsonConvert.DeserializeObject<List<kw>>(example);
Maybe you are not providing all details. Or your json string looks different.

Related

Properly deserialize object with RootObject [duplicate]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Facebook;
using Newtonsoft.Json;
namespace facebook
{
class Program
{
static void Main(string[] args)
{
var client = new FacebookClient(acc_ess);
dynamic result = client.Get("fql", new { q = "select target_id,target_type from connection where source_id = me()"});
string jsonstring = JsonConvert.SerializeObject(result);
//jsonstring {"data":[{"target_id":9503123,"target_type":"user"}]}
List<RootObject> datalist = JsonConvert.DeserializeObject<List<RootObject>>(jsonstring);
}
public class Datum
{
public Int64 target_id { get; set; }
public string target_type { get; set; }
}
public class RootObject
{
public List<Datum> data { get; set; }
}
}
}
Cannot deserialize the current JSON object (e.g. {"name":"value"})
into type
'System.Collections.Generic.List`1[facebook.Program+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) that can be
I looked at other posts.
My json looks like this:
{"data":[{"target_id":9503123,"target_type":"user"}]}
To make it clear, in addition to #SLaks' answer, that meant you need to change this line :
List<RootObject> datalist = JsonConvert.DeserializeObject<List<RootObject>>(jsonstring);
to something like this :
RootObject datalist = JsonConvert.DeserializeObject<RootObject>(jsonstring);
As the error message is trying very hard to tell you, you can't deserialize a single object into a collection (List<>).
You want to deserialize into a single RootObject.
Can you try to change your json without data key like below?
[{"target_id":9503123,"target_type":"user"}]
That happened to me too, because I was trying to get an IEnumerable but the response had a single value. Please try to make sure it's a list of data in your response. The lines I used (for api url get) to solve the problem are like these:
HttpResponseMessage response = await client.GetAsync("api/yourUrl");
if (response.IsSuccessStatusCode)
{
IEnumerable<RootObject> rootObjects =
awaitresponse.Content.ReadAsAsync<IEnumerable<RootObject>>();
foreach (var rootObject in rootObjects)
{
Console.WriteLine(
"{0}\t${1}\t{2}",
rootObject.Data1, rootObject.Data2, rootObject.Data3);
}
Console.ReadLine();
}
Hope It helps.
The real problem is that you are using dynamic return type in the FacebookClient Get method. And although you use a method for serializing, the JSON converter cannot deserialize this Object after that.
Use insted of:
dynamic result = client.Get("fql", new { q = "select target_id,target_type from connection where source_id = me()"});
string jsonstring = JsonConvert.SerializeObject(result);
something like that:
string result = client.Get("fql", new { q = "select target_id,target_type from connection where source_id = me()"}).ToString();
Then you can use DeserializeObject method:
var datalist = JsonConvert.DeserializeObject<List<RootObject>>(result);
Hope this helps.
public partial class tree
{
public int id { get; set; }
public string name { get; set; }
public string sciencename { get; set; }
public int familyid { get; set; }
}
private async void PopulateDataGridView()
{
//For Single object response
tree treeobj = new tree();
treeobj = JsonConvert.DeserializeObject<tree>(Response);
//For list of object response
List<tree> treelistobj = new List<tree>();
treelistobj = JsonConvert.DeserializeObject<List<tree>>(Response);
//done
}

Unable to Serialize/Deserialize List<> object into JSON

I am working on 2 web applications; A & B. now i have a shared class named CRUDOutput as follow on both web applications:-
public class CRUDOutput
{
public Operation4 operation { get; set; }
}
public class Operation4
{
public Result result { get; set; }
public string name { get; set; }
}
public class Result
{
public string status { get; set; }
public string message { get; set; }
}
now inside web application A i am returning the following:-
[HttpPost]
public ActionResult CreateResource(CreateResource cr)
{
List<CRUDOutput> co = new List<CRUDOutput>();
co.Add(JsonConvert.DeserializeObject<CRUDOutput>(crudoutput));
co.Add(JsonConvert.DeserializeObject<CRUDOutput>(crudoutput2));
return Json(JsonConvert.SerializeObject(co));
}
now from web application B, i am calling the action method as follow:-
try
{
using (WebClient wc = new WebClient())
{
string url = "https://localhost:44302/" + "Home/CreateResource";
Uri uri = new Uri(url);
wc.Headers.Add(HttpRequestHeader.ContentType, "application/json");
output = wc.UploadString(uri, data);
}
}
catch (WebException e)
{
}
List<CRUDOutput> result = JsonConvert.DeserializeObject<List< CRUDOutput>>(output);
but i will get the following exception when i tried to deserialize the output:-
Error converting value
"[{"operation":{"result":{"status":"Success","message":"Resource has
been added successfully to ......"},"name":"CREATE
RESOURCE"}},{"operation":{"result":{"status":"Failed","message":"Account
addition "},"name":"ADD ACCOUNTS"}}]" to type
'System.Collections.Generic.List`1[S.ViewModels.CRUDOutput]'. Path '',
line 1, position 464.
now the JSON return from web application A will be as follow:-
"\"[{\\\"operation\\\":{\\\"result\\\":{\\\"status\\\":\\\"Success\\\",\\\"message\\\":\\\"Resource 123 rfrf has been added successfully \\\"},\\\"name\\\":\\\"CREATE RESOURCE\\\"}},{\\\"operation\\\":{\\\"result\\\":{\\\"status\\\":\\\"Failed\\\",\\\"message\\\":\\\"Account addition \\\"},\\\"name\\\":\\\"ADD ACCOUNTS\\\"}}]\""
so can anyone advice why i am unable to deserialize to a list of objects?
The output as you've pasted is encoded as JSON twice. Compare the difference between:
"\"[{\\\"operation\\\":{\\\"result\\\":{\\\"status\\\":\\\"Success\\\",\\\"message\\\":\\\"Resource 123 rfrf has been added successfully \\\"},\\\"name\\\":\\\"CREATE RESOURCE\\\"}},{\\\"operation\\\":{\\\"result\\\":{\\\"status\\\":\\\"Failed\\\",\\\"message\\\":\\\"Account addition \\\"},\\\"name\\\":\\\"ADD ACCOUNTS\\\"}}]\""
and
"[{\"operation\":{\"result\":{\"status\":\"Success\",\"message\":\"Resource 123 rfrf has been added successfully \"},\"name\":\"CREATE RESOURCE\"}},{\"operation\":{\"result\":{\"status\":\"Failed\",\"message\":\"Account addition \"},\"name\":\"ADD ACCOUNTS\"}}]"
This happens because you're encoding the result as Json twice. Replace:
return Json(JsonConvert.SerializeObject(result));
with
return Json(result); // This encodes as JSON automatically

Searching for Filter in Jira-Rest-Api with C#

I am trying to search all favourite Filters (JIRA) from the actual User using c# HttpWebRequest and Rest-Api. I am still able to read Issues but the filters aren't working.
Reading Issues works as follows:
For example I have this URL to get all Issues from project IT:
http://jira-test.myServer.de/rest/api/2/search?jql=project=%22IT%22
I am using DataContractJsonSerializer to swap the JSON Response to C#-Objects.
From this class I am getting an object after Serialization:
[DataContract]
internal class Kopf
{
[DataMember]
public string startAt = string.Empty;
[DataMember]
public string maxResults = string.Empty;
[DataMember]
public string total = string.Empty;
[DataMember]
public Issues[] issues = null;
}
The first lines of JSON are looking like this:
{"expand":"schema,names","startAt":0,"maxResults":50,"total":23044,"issues":[{"expand":"operations,editmeta,changelog,transitions,renderedFields","id":"40000","self":"http://jira-test.myServer.de/rest/api/2/issue/40000","key":"IT-23237","fields":
So I can't understand why the following isn't working for me:
This URL give me the right JSON in Browser:
http://jira-test.myServer.de/rest/api/2/filter/favourite
First lines of JSON:
[{"self":"http://jira-test.myServer.de/rest/api/2/filter/10119","id":"10119","name":"Aktiv","description":"Alle Aufgaben die gerade aktiv von mir bearbeitet werden.","owner":{"self":"http://jira-test.myServer.de/rest/api/2/user?username=sb9923","key":"sb9923","name":"sb9923","avatarUrls":{"16x16":"http://jira-test.myServer.de/secure/useravatar?
And here is my Object which I want to serialize:
[DataContract]
internal class FilterData
{
[DataMember]
public FilterKopf[] filter = null;
}
[DataContract]
internal class FilterKopf
{
[DataMember]
public string id = string.Empty;
[DataMember]
public string name = string.Empty;
[DataMember]
public string description = string.Empty;
[DataMember]
public string jql = string.Empty;
}
I don't get any Exception or something but the FilterKopf Array in the FilterData-Object is always null.
I hope someone can help me with this. I think my C#-Class is the problem because the JSON seems fine and my browser gives the right output.
If I understand right your problem is that the result contains an array of "Filter" objects but you want to deserialize it as a simple object containing the array. So all you need is to deserialize the stream as FilterKopf[] instead of FilterData.
I created a simple request based on this answer (I modified it slightly, e.g. not POST but GET)
public class JiraTest
{
internal IEnumerable<FilterKopf> GetFavouriteFilters()
{
string url = "http://jira-test.myserver.de/rest/api/2/filter/favourite";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
httpWebRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("YOUR_USERNAME:YOUR_PASSWORD"));
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(FilterKopf[]));
var filterKoepfe = (FilterKopf[])serializer.ReadObject(httpResponse.GetResponseStream());
return filterKoepfe;
}
}
[DataContract]
internal class FilterKopf
{
[DataMember]
public string id = string.Empty;
[DataMember]
public string name = string.Empty;
[DataMember]
public string description = string.Empty;
[DataMember]
public string jql = string.Empty;
}
With my own account and with my access to our Jira server the results really reflected my favourite filters.
Update
As a second chance, try to use Json.NET instead of DataContractJsonSerializer. Add to the project through NuGet, and replace the two rows of deserialization to these:
FilterKopf[] filterKoepfe = null;
using (Stream stream = httpResponse.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
string jsonResponse = reader.ReadToEnd();
filterKoepfe = Newtonsoft.Json.JsonConvert.DeserializeObject<FilterKopf[]>(jsonResponse);
}
Let's take a look what this does.

Convert Json Singleton into List Phone 81 UAP

I am using http client to return a json response from a webservice. The example I am following here is from code project tutorial. However its example only returns into a var, it was created for the method to be called on screen I am changing it to be called from within a class. I have removed the webservice for security.
My Main question is how would I change this function to return a List of cinemas instead of the var variable I have a class created as such. I tried changing var into List but i noticed json.net handles this list so I need return the var as a known object instead I think?.
public class City
{
public string id { get; set; }
public string timing_title { get; set; }
}
public class Citys
{
public List<City> city { get; set; }
}
I just don't know what to do to convert this so it returns a list of citys for me to use in function.
This is a list of example json data returned.
{"city":[{"id":"5521","timing_title":"Lahore"},{"id":"5517","timing_title":"Karachi"},{"id":"5538","timing_title":"Islamabad"},{"id":"5535","timing_title":"Rawalpindi"},{"id":"5518","timing_title":"Hyderabad"},{"id":"5512","timing_title":"Faisalabad"},{"id":"8028","timing_title":"Gujranwala"},{"id":"8027","timing_title":"Gujrat"}]}
public async void GetCinemasList()
{
try
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("webserviceurl");
var url = "index.php/webservice/upcoming_movie";
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync();
var cityData = JsonConvert.DeserializeObject<City>(data.Result.ToString());
}
}
}
catch (Exception ex)
{
// MessageBox.Show("Some Error Occured");
}
}
You have a string holding a JSON value.
You can parse and iterate through JSON values like this:
var data = "{\"city\":[{\"id\":\"5521\",\"timing_title\":\"Lahore\"},{\"id\":\"5517\",\"timing_title\":\"Karachi\"},{\"id\":\"5538\",\"timing_title\":\"Islamabad\"},{\"id\":\"5535\",\"timing_title\":\"Rawalpindi\"},{\"id\":\"5518\",\"timing_title\":\"Hyderabad\"},{\"id\":\"5512\",\"timing_title\":\"Faisalabad\"},{\"id\":\"8028\",\"timing_title\":\"Gujranwala\"},{\"id\":\"8027\",\"timing_title\":\"Gujrat\"}]}";
JsonObject rootObject;
if (JsonObject.TryParse(data, out rootObject))
{
JsonArray cityArray = rootObject.GetNamedArray("city");
foreach (var jsonValue in cityArray)
{
JsonObject cityObject = jsonValue.GetObject();
Debug.WriteLine(cityObject.GetNamedString("timing_title"));
}
}
else
{
// Invalid JSON data.
}
Output:
Lahore
Karachi
Islamabad
Rawalpindi
Hyderabad
Faisalabad
Gujranwala
Gujrat

C# access to object property after json decode

after long hours of searching for a good JSON library, i found Newtownsoft.json, so i started using it to decode a json text i get from a web request, i don't know if the json is being decoded properly
the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//Request library
using System.Net;
using System.IO;
using Newtonsoft.Json;
namespace TestApplication
{
class Connect
{
public string id;
public string type;
private string api = "https://api.stackexchange.com/2.2/";
private string options = "?order=desc&sort=name&site=stackoverflow";
public object request()
{
string totalUrl = this.join(id);
string json = this.HttpGet(totalUrl);
return this.decodeJson(json);
}
private string join(string s)
{
return api + type + "/" + s + options;
}
private string HttpGet(string URI)
{
string html = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URI);
request.AutomaticDecompression = DecompressionMethods.GZip;
request.ContentType = "application/json; charset=utf-8";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
return html;
}
private object decodeJson(string json)
{
object js = JsonConvert.DeserializeObject(json);
return js;
}
}
}
the class object is being accessed from the form in this way:
Connect rq = new
rq.id = usernameText.Text;
rq.type = "users";
Debug.WriteLine(rq.request());
i don't know why i can't do rq.request().items or rq.request()["items"], i am still learning c# and i would like to know how to access the json object members in the proper way.
NOTE: this is the first desktop program i'm developing, i am a php/nodejs developer and i wanted to make an application that will connect to stack exchange database and retrieve user's info.
The return type of your request method is object, and so the returned instance will not have a property named items.
You'll need to use generic methods and specify the correct type parameter.
Try changing your decodeJson method to this:
private T decodeJson<T>(string json)
{
var js = JsonConvert.DeserializeObject<T>(json);
return js;
}
And then change your request method to this:
public T request<T>()
{
string totalUrl = this.join(id);
string json = HttpGet(totalUrl);
return decodeJson<T>(json);
}
Now write a class with properties that match the name and type of the properties in the JSON returned from the web request.
Then specify the type of this new class as the type parameter for your call to the request method.
For example, if you expected the JSON to contain a string called 'Name' and an int called 'Age', write a class that is something like this:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
and then call request like this
Person myPerson = rq.request<Person>();
and you'll be left with an instance of Person with Name and Age properties

Categories