I have a problem in working with JSON. I have a published JSON on the web. The content of my JSON is this now:
"[{\"Column\":4,\"GroupId\":2020,\"GroupTitle\":\"نرم افزار کاربردی\",\"Average\":0.0,\"Cost\":1000,\"IconSquare\":\"~\\\\Files\\\\82\\\\Programs\\\\ProPlayer_V0.0.1.0\\\\839fcb30-3dfc-4b6f-a826-57601dad5047.jpg\",\"IconWide\":\"~\\\\Files\\\\82\\\\Programs\\\\ProPlayer_V0.0.1.0\\\\26777d6a-025d-40a8-b83a-12fcb1f97ca4.jpg\",\"Id\":3,\"NameEnglish\":\"ProPlayer\",\"NamePersian\":\"پروپلیر\",\"Size\":2.75,\"Version\":\"0.0.1.0\",\"VersionWindowsPhone\":\"ویندوزفون 8.1 و بالاتر\",\"Score\":0.0,\"Availables\":\"ویدئو ها\\r\\nموزیک ها\",\"Description\":\"برنامه خوبیه اما در حد تست \",\"Develeoper\":\" \",\"DTPublication\":\"2015-08-23T17:20:31.46\",\"Star1\":0,\"Star2\":0,\"Star3\":0,\"Star4\":0,\"Star5\":0,\"ProgramPath\":\"~\\\\Files\\\\82\\\\Programs\\\\ProPlayer_V0.0.1.0\\\\ProPlayer_V0.0.1.0.appxbundle\"}]"
I used Json.Net from NewtonSoft. Here is my class to convert JSON to this class type :
public class AppOrGame
{
public int Column { get; set; }
public int GroupId { get; set; }
public string GroupTitle { get; set; }
public float Average { get; set; }
public int Cost { get; set; }
public Uri IconSquare { get; set; }
public Uri IconWide { get; set; }
public int Id { get; set; }
public string NameEnglish { get; set; }
public string NamePersian { get; set; }
public float Size { get; set; }
public string Version { get; set; }
public string VersionWindowsPhone { get; set; }
public float Score { get; set; }
public string Availables { get; set; }
public string Description { get; set; }
public string Develeoper { get; set; }
public DateTime DTPublication { get; set; }
public int Star1 { get; set; }
public int Star2 { get; set; }
public int Star3 { get; set; }
public int Star4 { get; set; }
public int Star5 { get; set; }
public Uri ProgramPath { get; set; }
}
.
And here is my code to download a JSON string and then convert :
public async Task<string> DownloadStringsFromWeb(string uri)
{
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
string res = "";
webrequest.Method = "GET";
using (var webresponse = await webrequest.GetResponseAsync())
using (StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream()))
{
res = loResponseStream.ReadToEnd();
}
try
{
var a = JsonConvert.DeserializeObject <List<MainPage.AppOrGame>> (res);
}
catch (Exception ex)
{
}
return res;
}
The DownloadStringsFromWeb is not the final function so I put the JSON converter in it too but I will separate them from each other. When I debug my code I get an exception on JsonConvert
{"Error converting value {Exact Json value I provided above}
to type 'System.Collections.Generic.List`1[Universal4Khune.MainPage+AppOrGame]'. Path '', line 1, position 818."}
The " (double quote) isn't a symbol to be escaped in JSON context. Fix of the issue is depends on how do you produce the JSON? If it is in the file, for example, just remove backslashes. To be more specific, I should know the nature of your JSON.
Related
Please excuse this question as a sign of frustration. While I understand that cause of the issue is the converting of data into different types, I can't put my finger on what's causing the result of this web service get method to be output incorrectly, that is containing backslashes.
The code below sums up the functionality of my method. I've tried different suggestions I found on these forums, some of which were in reply to similar questions I asked myself.
Data Model
public class WeatherResponse
{
public class Coord
{
public double lon { get; set; }
public double lat { get; set; }
}
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public class Main
{
public double temp { get; set; }
public int pressure { get; set; }
public int humidity { get; set; }
public int temp_min { get; set; }
public int temp_max { get; set; }
}
public class Wind
{
public double speed { get; set; }
public int deg { get; set; }
}
public class Clouds
{
public int all { get; set; }
}
public class Sys
{
public int type { get; set; }
public int id { get; set; }
public double message { get; set; }
public string country { get; set; }
public int sunrise { get; set; }
public int sunset { get; set; }
}
public class RootObject
{
public Coord coord { get; set; }
public List<WeatherResponse> weather { get; set; }
public string #base { get; set; }
public Main main { get; set; }
public int visibility { get; set; }
public Wind wind { get; set; }
public Clouds clouds { get; set; }
public int dt { get; set; }
public Sys sys { get; set; }
public int id { get; set; }
public string name { get; set; }
public int cod { get; set; }
}
}
Controller
[HttpGet]
public string Get(string city, string country)
{
string apiKey = "KEY";
HttpWebRequest apiRequest = WebRequest.Create("http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + country + " &appid=" + apiKey + "&units=metric") as HttpWebRequest;
string apiResponse = "";
using (HttpWebResponse response = apiRequest.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
apiResponse = reader.ReadToEnd();
}
Response.ContentType = "application/json";
return apiResponse;
}
Result
"{\"coord\":{\"lon\":-0.13,\"lat\":51.51},\"weather\":[{\"id\":521,\"main\":\"Rain\",\"description\":\"shower
rain\",\"icon\":\"09d\"}],\"base\":\"stations\",\"main\":{\"temp\":2.62,\"pressure\":991,\"humidity\":69,\"temp_min\":1,\"temp_max\":4},\"visibility\":10000,\"wind\":{\"speed\":5.1,\"deg\":90},\"clouds\":{\"all\":75},\"dt\":1548939000,\"sys\":{\"type\":1,\"id\":1414,\"message\":0.0037,\"country\":\"GB\",\"sunrise\":1548920401,\"sunset\":1548953318},\"id\":2643743,\"name\":\"London\",\"cod\":200}"
I need the code to simply retrieve weather data using the city and country received and correctly output it as JSON.
Here's the solution to the problem, as kindly posted by Joroen Mostert.
You should be able to use Content for this (when you change the return
type of your method to IActionResult): return Content(apiResponse,
"application/json") should not further escape.
I am having issues deserializing a nested JSON array from the Genius lyric website API. I formulated the object using http://json2csharp.com. When I deserialize the object, I am unable to access the properties inside of the class, which wasn't entirely unexpected, I am just not sure how to properly design an actual solution to the problem. The JSON object conversions work fine when they are not nested.
What would be the best way to go about handling this?
Here is the conversion code:
string test = await G.SearchGeniusASync(textBox1.Text);
var data = JsonConvert.DeserializeObject<GeniusApiObject>(test);
Here is my class:
class GeniusApiObject
{
public class Meta
{
public int status { get; set; }
}
public class Stats
{
public bool hot { get; set; }
public int unreviewed_annotations { get; set; }
public int concurrents { get; set; }
public int pageviews { get; set; }
}
public class PrimaryArtist
{
public string api_path { get; set; }
public string header_image_url { get; set; }
public int id { get; set; }
public string image_url { get; set; }
public bool is_meme_verified { get; set; }
public bool is_verified { get; set; }
public string name { get; set; }
public string url { get; set; }
public int iq { get; set; }
}
public class Result
{
public int annotation_count { get; set; }
public string api_path { get; set; }
public string full_title { get; set; }
public string header_image_thumbnail_url { get; set; }
public string header_image_url { get; set; }
public int id { get; set; }
public int lyrics_owner_id { get; set; }
public string lyrics_state { get; set; }
public string path { get; set; }
public int? pyongs_count { get; set; }
public string song_art_image_thumbnail_url { get; set; }
public Stats stats { get; set; }
public string title { get; set; }
public string title_with_featured { get; set; }
public string url { get; set; }
public PrimaryArtist primary_artist { get; set; }
}
public class Hit
{
public List<object> highlights { get; set; }
public string index { get; set; }
public string type { get; set; }
public Result result { get; set; }
}
public class Response
{
public List<Hit> hits { get; set; }
}
public class RootObject
{
public Meta meta { get; set; }
public Response response { get; set; }
}
}
This is the source for the SearchGeniusASync method in case it is helpful:
public async Task<string>SearchGeniusASync(string searchParameter)
{
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", clientAccessToken);
var result = await httpClient.GetAsync(new Uri("https://api.genius.com/search?q=" + searchParameter), HttpCompletionOption.ResponseContentRead);
var data = await result.Content.ReadAsStringAsync();
return data;
}
This is the scope I am given access to:
https://i.imgur.com/9mZMvfp.png
Here's a sample JSON request in plaintext:
https://pastebin.com/iA8dQafW
GeniusApiObject is not needed in the code, but I'll leave it in just because it helps organize things (may be that something else also has a RootObject from the auto-generator).
The problem is that you are trying to deserialize to what is just an empty class, the class itself has no properties, so you can't deserialize to it. You need to deserialize to the GeniusApiObject.RootObject.
var data = JsonConvert.DeserializeObject<GeniusApiObject.RootObject>(test);
Will deserialize to the .RootObject subclass. This is verified working:
Where I'm using File.ReadAllText("test.json") to load the example API data provided.
Here is a .NET Fiddle showing it working (without the root object and only one song in the response). Thanks to #maccttura.
I'm trying out the Philips Hue lights api for the first time and I have a few questions on how to deserialize the json string to a C# object. I'm trying this out for the Xamarin.iOS app I'm working on.
Here's my method that would fetch the light data from around me:
private string getLights()
{
var url = APIURL + APIKey + LightsEndPoint ;
var request = WebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "GET";
using (var response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
Console.Out.WriteLine(
"Error fetching data. Server returned status code: {0}",
response.StatusCode);
using (var reader = new StreamReader(response.GetResponseStream()))
{
var content = reader.ReadToEnd();
if(string.IsNullOrWhiteSpace(content))
{
Console.WriteLine("Response contained empty body...");
}
else
{
Console.WriteLine("Response Body: \r\n {0}", content);
var items = JsonConvert.DeserializeObject <Light> (content);
}
return content;
}
}
}
Where Light is:
public class Light
{
public Light()
{ }
public string LightName { get; set;}
[JsonProperty("state")]
public string IsOn { get; set; }
[JsonProperty("sat")]
public int Saturation { get; set; }
[JsonProperty("bri")]
public int Brightness {get;set;}
[JsonProperty("hue")]
public int Hue { get; set; }
}
The problem I'm having now is my items object is always empty, null values even though I'm properly getting content json string.
My json string looks like this:
{
"1":{
"state":{
"on":true,
"bri":254,
"hue":20000,
"sat":100,
"effect":"none",
"xy":[
0.4146,
0.4155
],
"ct":299,
"alert":"none",
"colormode":"hs",
"reachable":true
},
"type":"Extended color light",
"name":"Hue color lamp 1",
"modelid":"LCT007",
"manufacturername":"Philips",
"uniqueid":"00:17:88:01:10:26:3f:12-0b",
"swversion":"5.38.1.14919"
},
"2":{
"state":{
"on":false,
"bri":254,
"hue":50000,
"sat":254,
"effect":"none",
"xy":[
0.2468,
0.0843
],
"ct":153,
"alert":"none",
"colormode":"hs",
"reachable":true
},
"type":"Extended color light",
"name":"Hue color lamp 2",
"modelid":"LCT007",
"manufacturername":"Philips",
"uniqueid":"00:17:88:01:10:5d:fd:f6-0b",
"swversion":"5.38.1.14919"
},
"3":{
"state":{
"on":true,
"bri":254,
"hue":10000,
"sat":254,
"effect":"none",
"xy":[
0.5711,
0.3986
],
"ct":500,
"alert":"none",
"colormode":"hs",
"reachable":true
},
"type":"Extended color light",
"name":"Hue color lamp 3",
"modelid":"LCT007",
"manufacturername":"Philips",
"uniqueid":"00:17:88:01:10:26:3d:17-0b",
"swversion":"5.38.1.14919"
}
}
The problem I'm having is light is indicated by a numerical value and I'm not sure how to split the json string to populate my c# object.
Basically, I'm having issues converting the json string to c# object for api stream for Xamarin.iOS app.
Your model should look like this
public class Light
{
public Light()
{
}
[JsonProperty("name")]
public string LightName { get; set;}
[JsonProperty("state")]
public State State { get; set; }
}
public class State
{
public State()
{
}
[JsonProperty("on")]
public bool IsOn { get; set; }
[JsonProperty("sat")]
public int Saturation { get; set; }
[JsonProperty("bri")]
public int Brightness {get;set;}
[JsonProperty("hue")]
public int Hue { get; set; }
}
And deserialization call should look like this
JsonConvert.DeserializeObject<Dictionary<string, Light>>(content);
Where the key of the dictionary is the numbers and value is the light model you want to get.
I generated a class with json2csharp:
public class State
{
public bool on { get; set; }
public int bri { get; set; }
public int hue { get; set; }
public int sat { get; set; }
public string effect { get; set; }
public List<double> xy { get; set; }
public int ct { get; set; }
public string alert { get; set; }
public string colormode { get; set; }
public bool reachable { get; set; }
}
public class RootObject
{
public State state { get; set; }
public string type { get; set; }
public string name { get; set; }
public string modelid { get; set; }
public string manufacturername { get; set; }
public string uniqueid { get; set; }
public string swversion { get; set; }
}
then I called this code:
var a = JsonConvert.DeserializeObject<Dictionary<string, RootObject>>(json);
and the result was this:
As mentioned, your model structure does not match that of the json's. You need to properly nest the properties accordingly. I threw together an example, though certain data types I was unsure of I simply used a string(Which should be fine).
Light.cs
public class Light
{
public string LightName { get; set; }
[JsonProperty("state")]
public State LightState { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("modelid")]
public string ModelId { get; set; }
[JsonProperty("manufacturername")]
public string Manufacturer { get; set; }
[JsonProperty("uniqueid")]
public string UniqueId { get; set; }
[JsonProperty("swversion")]
public string SwVersion { get; set; }
}
State.cs
public class State
{
[JsonProperty("on")]
public bool IsOn { get; set; }
[JsonProperty("bri")]
public int Brightness { get; set; }
[JsonProperty("hue")]
public int Hue { get; set; }
[JsonProperty("sat")]
public int Saturation { get; set; }
[JsonProperty("effect")]
public string Effect { get; set; } // Just making it a string for now
[JsonProperty("xy")]
public double[] XY { get; set; }
[JsonProperty("ct")]
public int CT { get; set; }
[JsonProperty("alert")]
public string Alert { get; set; } // Just making another string for now
[JsonProperty("colormode")]
public string ColorMode { get; set; } // Hey, it's another string for now
[JsonProperty("reachable")]
public bool Reachable { get; set; }
}
Then to deserialize:
var items = JsonConvert.DeserializeObject<Dictionary<string, Light>> (content);
I'm new to Json with C#. I`m trying to deserialize JSON string to display to into a data grid.
I succeeded to get the JSON string from the server but when trying to deserialize it, it throws this exception:
Newtonsoft.Json.JsonSerializationException: Error converting value
"id" to type 'Eng_Tab.JsonData'. Path '[0]', line 1, position 5. --->
System.ArgumentException: Could not cast or convert from System.String
to Eng_Tab.JsonData.
This is the data class:
public class JsonData
{
public int id { get; set; }
public string lec { get; set; }
public string sec1 { get; set; }
public string sec2 { get; set; }
public string sec3 { get; set; }
public string sec4 { get; set; }
public string sec5 { get; set; }
public string sec6 { get; set; }
public string sec7 { get; set; }
public string sec8 { get; set; }
public string sec9 { get; set; }
public string sec10 { get; set; }
public int h { get; set; }
public int h1 { get; set; }
public int h2 { get; set; }
public int h3 { get; set; }
public int h4 { get; set; }
public int h5 { get; set; }
public int h7 { get; set; }
public int h8 { get; set; }
public int h9 { get; set; }
public int h10 { get; set; }
public int m { get; set; }
public int m1 { get; set; }
public int m2 { get; set; }
public int m3 { get; set; }
public int m4 { get; set; }
public int m5 { get; set; }
public int m6 { get; set; }
public int m7 { get; set; }
public int m8 { get; set; }
public int m9 { get; set; }
public int m10 { get; set; }
}
Here is the Json string:
["id":"1","h":"7","m":"0","lec":"","h1":"0","m1":"0","sec1":"","h2":"10","m2":"0","sec2":"Abdelrahman
Mohamed401119343000","h3":"10","m3":"0","sec3":"Abdelrahman
Mohamed401119343000","h4":"5","m4":"0","sec4":"A401119343000","h5":"5","m5":"0","sec5":"A401119343000","h6":"5","m6":"0","sec6":"A401119343000","h7":"5","m7":"0","sec7":"A401119343000","h8":"5","m8":"0","sec8":"A401119343000","h9":"18","m9":"0","sec9":"Abdelrahman
Mohamed401119343000","h10":"0","m10":"0","sec10":"set sec"]
and this is my C# code:
string qry = "de=e1&id=1";
WebRequest request =WebRequest.Create("the php link"+qry);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.ASCII);
string jsonData = reader.ReadToEnd();
jsonData = jsonData.Replace("{", "[").Replace("}","]");
MessageBox.Show(jsonData);
List<JsonData> result = JsonConvert.DeserializeObject<List<JsonData>>(jsonData);
metroGrid1.DataSource = result;
You need on object of type JsonData in Json string to deserialize. Thus your JSON string should look like [{"ABC":"PQR", ...}]
How do I parse a Json response in form of:
www.extradelar.se/match
If I understand this response right, its an array of three responses, how do I parse them in this case? How do I Deserialize this into my RootObject?
I am not sure if its due to copy paste but the json provided was not valid:
Using http://jsonlint.com/ you can validate an indent your json:
Once you indent, it is easier to look at .
The above JSON is array of array where each one contains an object.
It is a little odd for usual JSON, but maybe you have your own reasons.
Using libraries like JSON.net you can easily parse that data into C# objects.
Hope this helps
EDIT:
POCO Class:
public class RootObject
{
public string match_id { get; set; }
public string no_repick { get; set; }
public string no_agi { get; set; }
public string drp_itm { get; set; }
public string no_timer { get; set; }
public string rev_hs { get; set; }
public string no_swap { get; set; }
public string no_int { get; set; }
public string alt_pick { get; set; }
public string veto { get; set; }
public string shuf { get; set; }
public string no_str { get; set; }
public string no_pups { get; set; }
public string dup_h { get; set; }
public string ap { get; set; }
public string br { get; set; }
public string em { get; set; }
public string cas { get; set; }
public string rs { get; set; }
public string nl { get; set; }
public string officl { get; set; }
public string no_stats { get; set; }
public string ab { get; set; }
public string hardcore { get; set; }
public string dev_heroes { get; set; }
public string verified_only { get; set; }
public string gated { get; set; }
}
JSON.NET
private string getMatchId()
{
using (var webClient = new System.Net.WebClient())
{
const string url = #"http://www.extradelar.se/match";
var json = webClient.DownloadString(url);
var matchen = JsonConvert.DeserializeObject<List<List<RootObject>>>(json);
var matchId = matchen[0][0].match_id;
return matchId;
}
}