I have a JSON document and I want to access the details of the STATUS SECTION but it keeps returning null.
JSON Data is as shown:
{
"results":[
{
"messageId":"15712480583306574",
"to":"",
"from":"TestServer",
"sentAt":"2019-10-16T17:47:38.368+0000",
"doneAt":"2019-10-16T17:47:38.370+0000",
"smsCount":1,
"mccMnc":"null",
"price":{
"pricePerMessage":0.0,
"currency":"USD"
},
"status":{
"groupId":5,
"groupName":"REJECTED",
"id":8,
"name":"REJECTED_PREFIX_MISSING",
"description":"Number prefix missing"
},
"error":{
"groupId":0,
"groupName":"OK",
"id":0,
"name":"NO_ERROR",
"description":"No Error",
"permanent":false
}
}
]
}
C# Code is:
string JsonData = response.Content.ToString();
dynamic results = JsonConvert.DeserializeObject<dynamic>(JsonData);
var statuses = results.status;
foreach(var stat in statuses) {
string groupname = stat.groupName.Value;
string name = stat.name.Value;
string description = stat.description.Value;
}
It keeps returning null, How can I access these members? I am using Newtonsoft.
If you want to access the status object property you need to rewrite your whole code.
string JsonData = response.Content.ToString();
var input = JObject.Parse(str);
var results = input["results"].Children();
var status = results.First()["status"];
string groupname = status["groupName"].ToString();
string name = status["name"].ToString();
string description = status["description"].ToString();
Console.WriteLine(groupname);
Console.WriteLine(name);
Console.WriteLine(description);
The result in Console
REJECTED
REJECTED_PREFIX_MISSING
Number prefix missing
But I would rather use concrete class. You need to create multiple classes. Here is good example.
public class Envelope
{
public List<Item> Results { get; set; }
}
public class Item
{
public Status Status { get; set; }
}
public class Status
{
public int GroupId { get; set; }
public string GroupName { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
After that the usage is much simpler.
string JsonData = response.Content.ToString();
MyEnvelope envelope = JsonConvert.DeserializeObject<MyEnvelope>(JsonData);
var status = envelope.results[0].status;
Console.WriteLine(status.GroupName);
Console.WriteLine(status.Name);
Console.WriteLine(status.Description);
Finest Option: Create A model for the JSON.
public class Price
{
public double pricePerMessage { get; set; }
public string currency { get; set; }
}
public class Status
{
public int groupId { get; set; }
public string groupName { get; set; }
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
}
public class Error
{
public int groupId { get; set; }
public string groupName { get; set; }
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public bool permanent { get; set; }
}
public class Result
{
public string messageId { get; set; }
public string to { get; set; }
public string from { get; set; }
public DateTime sentAt { get; set; }
public DateTime doneAt { get; set; }
public int smsCount { get; set; }
public string mccMnc { get; set; }
public Price price { get; set; }
public Status status { get; set; }
public Error error { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
}
Then do RootObject results = JsonConvert.DeserializeObject<RootObject>(JsonData);
Fair Option: Get the exact JToken you want.
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Linq;
public class Program
{
public static void Main()
{
string jsonData = "{\"results\":[{\"messageId\":\"15712480583306574\",\"to\":\"\",\"from\":\"TestServer\",\"sentAt\":\"2019-10-16T17:47:38.368+0000\",\"doneAt\":\"2019-10-16T17:47:38.370+0000\",\"smsCount\":1,\"mccMnc\":\"null\",\"price\":{\"pricePerMessage\":0.0,\"currency\":\"USD\"},\"status\":{\"groupId\":5,\"groupName\":\"REJECTED\",\"id\":8,\"name\":\"REJECTED_PREFIX_MISSING\",\"description\":\"Number prefix missing\"},\"error\":{\"groupId\":0,\"groupName\":\"OK\",\"id\":0,\"name\":\"NO_ERROR\",\"description\":\"No Error\",\"permanent\":false}}]}";
JObject jObject = JObject.Parse(jsonData);
Console.WriteLine(jObject.SelectToken("results[0].status"));
}
}
Related
I am trying to receive data from the Random User API (https://api.randomuser.me/) with C# (which I am new to). I have a React front end and am able to successfully retrieve and render gender of a person, location of a person. However, I am struggling when it comes to the details that are further nested, such as the first name of a person. My backend code at the moment is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace RandomPersonGenerator.Controllers
{
[Route("api/[controller]")]
public class GeneratorController : Controller
{
[HttpGet("[action]")]
public async Task<IActionResult> Generate()
{
using (var client = new HttpClient())
{
try
{
client.BaseAddress = new Uri("https://api.randomuser.me");
var response = await client.GetAsync("https://api.randomuser.me");
response.EnsureSuccessStatusCode();
var stringResult = await response.Content.ReadAsStringAsync();
var rawData = JsonConvert.DeserializeObject<PersonAPIResponse>(stringResult);
return Ok(new
{
Gender = rawData.Results.Select(x => x.Gender)
});
}
catch (HttpRequestException httpRequestException)
{
return BadRequest($"Error generating person: {httpRequestException.Message}");
}
}
}
}
public class PersonAPIResponse
{
public IEnumerable<PersonDescription> Results { get; set; }
}
public class PersonDescription
{
public string Gender { get; set; }
}
}
I have tried to retrieve the first name of a person by adding:
Name = rawData.Results.Select(x => x.Name) and Name = rawData.Results.Select(x => x.Name.First) but this is not retrieving the data. Is anyone able to help me select the first name from the Random User API JSON?
Thank you!
Your problem is that you need to change this line:
var rawData = JsonConvert.DeserializeObject<PersonAPIResponse>(stringResult);
to
RootObject person = JsonConvert.DeserializeObject<RootObject>(stringResult);
You should create a new return type class and map into that, what you want to return.. assigning from person:
public class PersonAPIResponse
{
//.... your own properties
}
Return
return Ok(new PersonAPIResponse
{
Gender = person.results[0].gender, //first result
});
You also need to include the following classes for deserializing the string:
public class Name
{
public string title { get; set; }
public string first { get; set; }
public string last { get; set; }
}
public class Coordinates
{
public string latitude { get; set; }
public string longitude { get; set; }
}
public class Timezone
{
public string offset { get; set; }
public string description { get; set; }
}
public class Location
{
public string street { get; set; }
public string city { get; set; }
public string state { get; set; }
public int postcode { get; set; }
public Coordinates coordinates { get; set; }
public Timezone timezone { get; set; }
}
public class Login
{
public string uuid { get; set; }
public string username { get; set; }
public string password { get; set; }
public string salt { get; set; }
public string md5 { get; set; }
public string sha1 { get; set; }
public string sha256 { get; set; }
}
public class Dob
{
public DateTime date { get; set; }
public int age { get; set; }
}
public class Registered
{
public DateTime date { get; set; }
public int age { get; set; }
}
public class Id
{
public string name { get; set; }
public object value { get; set; }
}
public class Picture
{
public string large { get; set; }
public string medium { get; set; }
public string thumbnail { get; set; }
}
public class Result
{
public string gender { get; set; }
public Name name { get; set; }
public Location location { get; set; }
public string email { get; set; }
public Login login { get; set; }
public Dob dob { get; set; }
public Registered registered { get; set; }
public string phone { get; set; }
public string cell { get; set; }
public Id id { get; set; }
public Picture picture { get; set; }
public string nat { get; set; }
}
public class Info
{
public string seed { get; set; }
public int results { get; set; }
public int page { get; set; }
public string version { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
public Info info { get; set; }
}
{
public class Name
{
public string Title { get; set; }
public string First { get; set; }
public string Last { get; set; }
}
public class Result
{
public Name Name { get; set; }
}
public class Person
{
public List<Result> Results { get; set; }
}
public async Task<Name> GetPersonAsync()
{
HttpClient client = new HttpClient
{
BaseAddress = new Uri("https://api.randomuser.me")
};
HttpResponseMessage response = await client.GetAsync("https://randomuser.me/api/");
response.EnsureSuccessStatusCode();
var stringResult = await response.Content.ReadAsStringAsync();
Person root = JsonConvert.DeserializeObject<Person>(stringResult);
Console.WriteLine(root.Results[0].Name.Last);
return root.Results[0].Name;
}
}
I want to insert into my class data from JSON but I got a Newtonsoft.Json.JsonSerializationException.
My PlayerStats class is good, I think, and I don't know why it's not working.
I can download and print JSON to the console but my code stops working at the point when I try to deserialize. I tried to add settings to deserialize but it's still not working.
Here is my code:
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Net;
namespace DiscordBot
{
class Osu
{
public string _nickname;
private string _key = "key";
public class PlayerStats
{
[JsonProperty("user_id")]
public int UserId { get; set; }
[JsonProperty("username")]
public string UserName { get; set; }
[JsonProperty("count300")]
public int Count300 { get; set; }
[JsonProperty("count100")]
public int Count100 { get; set; }
[JsonProperty("count50")]
public int Count50 { get; set; }
[JsonProperty("playcount")]
public int PlayCount { get; set; }
[JsonProperty("ranked_score")]
public long RankedScore { get; set; }
[JsonProperty("total_score")]
public long TotalScore { get; set; }
[JsonProperty("pp_rank")]
public int PpRank { get; set; }
[JsonProperty("level")]
public double Level { get; set; }
[JsonProperty("pp_raw")]
public double RawPp { get; set; }
[JsonProperty("accuracy")]
public double Accuracy { get; set; }
[JsonProperty("count_rank_ss")]
public int CountRankSs { get; set; }
[JsonProperty("count_rank_ssh")]
public int CoundRankSsh { get; set; }
[JsonProperty("count_rank_s")]
public int CountRankS { get; set; }
[JsonProperty("count_rank_sh")]
public int CountRankSh { get; set; }
[JsonProperty("count_rank_a")]
public int CountRankA { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("pp_country_rank")]
public int PpCountryRank { get; set; }
}
public PlayerStats GetUserStats()
{
string json = string.Empty;
var result = JsonConvert.DeserializeObject<PlayerStats>(json);
try
{
string url = #"https://osu.ppy.sh/api/get_user";
using (WebClient wc = new WebClient())
{
wc.QueryString.Add("k", _key);
wc.QueryString.Add("u", _nickname);
wc.QueryString.Add("m", "0");
json = wc.DownloadString(url);
Console.WriteLine(json);
result = JsonConvert.DeserializeObject<PlayerStats>(json);
return result;
}
}
catch (WebException ex)
{
Console.WriteLine("Osu Error: " + ex.Status);
}
return result;
}
}
}
JSON:
[
{
"user_id":"10415972"
,"username":"iGruby"
,"count300":"851431"
,"count100":"15449 6"
,"count50":"19825"
,"playcount":"7129"
,"ranked_score":"453511877"
,"total_score" :"2735863526"
,"pp_rank":"147461"
,"level":"74.5611"
,"pp_raw":"1642.73"
,"accuracy" :"94.46521759033203"
,"count_rank_ss":"13"
,"count_rank_ssh":"2"
,"count_rank_s":"3 6"
,"count_rank_sh":"13"
,"count_rank_a":"65"
,"country":"PL"
,"pp_country_rank":"77 20"
,"events":[]
}
]
Refer this Tested Answer from the file I have loaded your json and DeserializeObject check my model also and how I am DeserializeObject
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string data = File.ReadAllText("D://readjson.txt");
var obj = JsonConvert.DeserializeObject<List<RootObject>>(data);
}
}
public class RootObject
{
public string user_id { get; set; }
public string username { get; set; }
public string count300 { get; set; }
public string count100 { get; set; }
public string count50 { get; set; }
public string playcount { get; set; }
public string ranked_score { get; set; }
public string total_score { get; set; }
public string pp_rank { get; set; }
public string level { get; set; }
public string pp_raw { get; set; }
public string accuracy { get; set; }
public string count_rank_ss { get; set; }
public string count_rank_ssh { get; set; }
public string count_rank_s { get; set; }
public string count_rank_sh { get; set; }
public string count_rank_a { get; set; }
public string country { get; set; }
public string pp_country_rank { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<object> events { get; set; }
}
}
Pp_country_rank and count_rank_s contains a space. This is trying to be parsed to an integer.
"count_rank_s":"3 6"
,"count_rank_sh":"13"
,"count_rank_a":"65"
,"country":"PL"
,"pp_country_rank":"77 20"
public List<CoinMarket> GetCoinMarket()
{
List<CoinMarket> coinMarket = new List<CoinMarket>();
var URLWebAPI = "http://190.202.54.19/wsZeus/api/Account/Markets/Get";
try
{
using (var Client = new System.Net.Http.HttpClient())
{
var JSON = Client.GetStringAsync(URLWebAPI);
coinMarket = (List<CoinMarket>)Newtonsoft.Json.JsonConvert.DeserializeObject(JSON.Result);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(#" ERROR {0}", ex.Message);
}
return coinMarket;
}
It is throwing and i do not know why. It looks like there is something wrong with the serialization part. But i verified it.
You are using json deserializer incorrectly. DeserializeObject returns custom object which is not castable to your List<T>. This output:
Newtonsoft.Json.Linq.JArray
SO20171129.CoinData[]
System.Collections.Generic.List`1[SO20171129.CoinData]
is the result of this code.
class Program
{
static void Main(string[] args)
{
// returns Newtonsoft.Json.Linq.JArray
var coinMarket = Newtonsoft.Json.JsonConvert.DeserializeObject(File.ReadAllText("get.json"));
Console.WriteLine(coinMarket.GetType());
// returns array of CoinData
var coinMarketTyped = Newtonsoft.Json.JsonConvert.DeserializeObject<CoinData[]>(File.ReadAllText("get.json"));
Console.WriteLine(coinMarketTyped.GetType());
// returns List of CoinData
var coinMarketTyped2 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CoinData>>(File.ReadAllText("get.json"));
Console.WriteLine(coinMarketTyped2.GetType());
}
}
public class CoinData
{
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; }
public string price_btc { get; set; }
public string __invalid_name__24h_volume_usd { 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; }
}
My guess is that your json structure members do not match the CoinMarket class members.
What I would do is to copy the json result and generate the corresponding CoinMarket class on the following website: http://json2csharp.com/
It will output the right CoinMarket class for you.
public class CoinMarket
{
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; }
public string price_btc { get; set; }
[JsonProperty("24h_volume_usd")]
public string h_volume_usd { 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; }
}
}
This is my CoinMarket Class
I have the following Json format returned from a WebAPI. Can you guys help to deserialize please?.
{
"#odata.context":"http://....... ","value":[
**{
"RecordNumber":"LDxxxx","RecordType":"Loan","PropertyAddress":{ "Address1":"601 xxxx","Address2":null,"Zip":"99999","City":"abc","State":"ab","County":"abcd" }
,"Summary":{ "BorrowerName":null,"ProductCode":null,"Status":"Not Registered" }
}**,{
"RecordNumber":"LDxxxx","RecordType":"Loan","PropertyAddress":{ "Address1":"601 xxxx","Address2":null,"Zip":"99999","City":"abc","State":"ab","County":"abcd" }
,"Summary":{ "BorrowerName":null,"ProductCode":null,"Status":"Not Registered" }
},
….]
}
I need what's in the value element. The bold is what is repeated in the return from API. I created a class that matches the description as below.
public class RootObject
{
[JsonProperty(PropertyName = "RecordNumber")]
public string RecordNumber { get; set; }
[JsonProperty(PropertyName = "RecordType")]
public string RecordType { get; set; }
[JsonProperty(PropertyName = "PropertyAddress")]
public PropertyAddress PropertyAddress { get; set; }
[JsonProperty(PropertyName = "Summary")]
public Summary Summary { get; set; }
}
Was able to skip the first record in the Json array using the following, got the "Value" part....but have not been successful in retrieving the "Value" object
var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(forecast);
foreach (var kv in dict.Skip(1))
{
JArray jsonVal = JArray.Parse(kv.Value.ToString());
}
Appreciate your help.
Satya
You can deserialize to concrete classes (with the help of http://json2csharp.com/)
var result = JsonConvert.DeserializeObject<SOTest.Result>(json);
public class SOTest
{
public class PropertyAddress
{
public string Address1 { get; set; }
public object Address2 { get; set; }
public string Zip { get; set; }
public string City { get; set; }
public string State { get; set; }
public string County { get; set; }
}
public class Summary
{
public object BorrowerName { get; set; }
public object ProductCode { get; set; }
public string Status { get; set; }
}
public class Value
{
public string RecordNumber { get; set; }
public string RecordType { get; set; }
public PropertyAddress PropertyAddress { get; set; }
public Summary Summary { get; set; }
}
public class Result
{
[JsonProperty("#odata.context")]
public string Context { get; set; }
public List<Value> Value { get; set; }
}
}
I have class which return json string but I want to deserilize it into C# List Objects. My current code look like this
public class JsonBuilder
{
public static string BuildJson(DateTime fromDate, DateTime toDate)
{
var list = new List<dynamic>();
// create list with json object from service
var jsonObjList = JsonConvert.SerializeObject(list);
var des = (List<JsonObject>)JsonConvert.DeserializeObject(jsonObjList, typeof(List<JsonObject>));
return JsonConvert.SerializeObject(list);
}
Exception thrown when it tries to deserialize the "serialized" json string
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Error converting value
InnerException:
{"Could not cast or convert from System.String to MvcWebApp.Models.JsonObject."}
Have you tried this:
var des = (List<DeserializeObjects>)JsonConvert.DeserializeObject(jsonObjList, jsonObjList.GetType()));
or this:
var des = (List<DeserializeObjects>)JsonConvert.DeserializeObject(jsonObjList, typeof(List<dynamic>));
else this post could also help you to achieve your goal:
Deserialize json object into dynamic object using Json.net
try this
var jsonObjList = JsonConvert.SerializeObject(list);
dynamic resultList = JsonConvert.DeserializeObject(jsonObjList);
I would try with this class
public class Author
{
public int id { get; set; }
public string slug { get; set; }
public string name { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string nickname { get; set; }
public string url { get; set; }
public string description { get; set; }
}
public class CustomFields
{
public List<string> tags { get; set; }
}
public class Post
{
public int id { get; set; }
public string type { get; set; }
public string slug { get; set; }
public string url { get; set; }
public string status { get; set; }
public string title { get; set; }
public string title_plain { get; set; }
public string content { get; set; }
public string excerpt { get; set; }
public string date { get; set; }
public string modified { get; set; }
public List<object> categories { get; set; }
public List<object> tags { get; set; }
public Author author { get; set; }
public List<object> comments { get; set; }
public List<object> attachments { get; set; }
public int comment_count { get; set; }
public string comment_status { get; set; }
public CustomFields custom_fields { get; set; }
}
public class YourObject
{
public string status { get; set; }
public int count { get; set; }
public int count_total { get; set; }
public int pages { get; set; }
public List<Post> posts { get; set; }
}
Then, you deserialize with this :
var yourObject = JsonConvert.DeserializeObject<YourObject>(json);