I am using multiple levels of JSON data coming into php from a C# application, as in:
public class User_Group
{
public int ID_UserGroup { get; set; }
public string Name_UserGroup { get; set; }
public int UserID { get; set; }
}
public class User_Role
{
public int ID_User { get; set; }
public string Role_User { get; set; }
public string User_Role_Description { get; set; }
public List<User_Group> UserGroup { get; set; }
}
public class Stand_Orte
{
public int ID { get; set; }
public string Bezeichnung { get; set; }
public List<Modul> modul { get; set; }
}
public class Modul
{
public string ID { get; set; }
public string Seriennummer { get; set; }
public string Bezeichnung { get; set; }
public string StandortID { get; set; }
public List<Mess_Kanal> MessKanal { get; set; }
}
public class Mess_Kanal
{
public string ID { get; set; }
public string ModulID { get; set; }
public List<LogMess_Daten> LogMessDaten { get; set; }
}
public class LogMess_Daten
{
public string KanalID { get; set; }
public string Zeitstempel { get; set; }
}
public class RootObject
{
public int ID_Project { get; set; }
public string Name_Project { get; set; }
public int Receiver_ID { get; set; }
public string Receiver_Name { get; set; }
public int UserID { get; set; }
public User_Role UserRole { get; set; }
public Stand_Orte Standorte { get; set; }
}
I have an issue with accessing the 3rd level data elements.
For Eg., I am able to get the values uptil Project-> Stand_Orte-> Modul. but after that Project-> Stand_Orte-> Modul-> MessKanal Throws an error in PHP as " Trying to get property of non-object ".
I tried the following:
$phpArray['project']->Standorte[0]->modul[0]->MessKanal[0]->ID
$messkanals=$phpArray->Standorte->modul->MessKanal;
And then used a "foreach ($messkanals as $messkanal)" to insert MessKanal data into MYSQL.
it gives me the following errors respectively.
Cannot use object of type stdClass as array
Trying to get property of non-object
does anyone have any idea?
Thanks,
Revathy
Related
Trying best how to take the following classes and deserialize a Json file to return each class values back to Simpl+. I am able to receive the Total value but anything in a list I am at a lost.
public class Client
{
public string clientId { get; set; }
public string locale { get; set; }
public string location { get; set; }
public string auxiliaryId { get; set; }
public string description { get; set; }
public string type { get; set; }
public string typeDescription { get; set; }
public Hardware hardware { get; set; }
public Network network { get; set; }
}
public class Hardware
{
public string type { get; set; }
public string softwareVersion { get; set; }
public string serialNumber { get; set; }
public string hardwareVersion { get; set; }
public string model { get; set; }
}
public class Network
{
public string ip { get; set; }
public string mac { get; set; }
public object homepage { get; set; }
public string dhcpSubnet { get; set; }
}
public class Result
{
public List<Client> clients { get; set; }
public string total { get; set; }
public int limit { get; set; }
public int page { get; set; }
}
public class Root
{
public string jsonrpc { get; set; }
public object id { get; set; }
public Result result { get; set; }
}
I was able to resolve this by adding the following to the foreach statement:
args.MyIndex = (ushort)(rootObject.TriplePlayResult.clients.IndexOf(item) + 1);
I need to extract data from a JSON string from a specific url (this).
When I convert it into C# by using jsontocsharp.com it looks like this:
public class Publisher
{
public int id { get; set; }
public string name { get; set; }
}
public class Title
{
public int id { get; set; }
public string name { get; set; }
public int subscribers { get; set; }
}
public class Issue
{
public List<object> alternates { get; set; }
public string complete_title { get; set; }
public string cover_image { get; set; }
public string description { get; set; }
public string diamond_id { get; set; }
public int id { get; set; }
public bool is_parent { get; set; }
public double? issue_number { get; set; }
public string other { get; set; }
public double price { get; set; }
public Publisher publisher { get; set; }
public string release_date { get; set; }
public Title title { get; set; }
}
public class RootObject
{
public int count { get; set; }
public string date { get; set; }
public List<Issue> issues { get; set; }
public int next { get; set; }
public object prev { get; set; }
public int total { get; set; }
}
Now I need to use these as my classes so I can gather and use data on further layers of my project. But I don't know how should I put the url in my code. I think this is a very basic question but I couldn't quite get the answer I'm looking for in the other questions. Any help greatly appreciated thank you.
I have the following class object structure:
public class StatusObj3
{
public int status { get; set; }
public DataObj3 data { get; set; }
}
public class DataObj3
{
public string survey_id { get; set; }
public string date_created { get; set; }
public string date_modified { get; set; }
public int custom_variable_count { get; set; }
public List<Custom_VariablesObj> custom_variables { get; set; }
public int language_id { get; set; }
public int num_responses { get; set; }
public int question_count { get; set; }
public string nickname { get; set; }
public TitleObj title { get; set; }
public List<PagesObj> pages { get; set; }
}
public class Custom_VariablesObj
{
public string variable_label { get; set; }
public string question_id { get; set; }
}
public class TitleObj
{
public bool enabled { get; set; }
public string text { get; set; }
}
public class PagesObj
{
string page_id { get; set; }
string heading { get; set; }
string sub_heading { get; set; }
public List<QuestionObj> questions { get; set; }
}
public class QuestionObj
{
public string question_id { get; set; }
public string heading { get; set; }
public string position { get; set; }
public QuestionTypeObj type { get; set; }
public List<AnswerObj> answers { get; set; }
}
public class QuestionTypeObj
{
public string family { get; set; }
public string subtype { get; set; }
}
public class AnswerObj
{
public string answer_id { get; set; }
public int position { get; set; }
public string text { get; set; }
public string type { get; set; }
public bool visible { get; set; }
public int weight { get; set; }
public bool apply_all_rows { get; set; }
public bool is_answer { get; set; }
public List<ItemsObj> items { get; set; }
}
public class ItemsObj
{
public string answer_id { get; set; }
public int position { get; set; }
public string type { get; set; }
public string text { get; set; }
}
I 've deserialized json into this object via:
var results_SurveyDetails = deserializer.Deserialize<StatusObj3>(json_returned);
I'm trying to loop thru the pages by:
foreach (var page in results_SurveyDetails.data.pages)
However, not all members of page are available to use.
Only the page.questions is there and not page_id, heading and subheading.
What am I doing wrong?
You are missing public on your other properties in class PagesObj.
I have a class with nested subclasses:
public class listDevicesModel
{
public int totalCount { get; set; }
public Messages messages { get; set; }
public Devices devices { get; set; }
public class Entry
{
public string key { get; set; }
public object value { get; set; }
}
public class Detail
{
public List<Entry> entry { get; set; }
}
public class Devices
{
public List<Device> device { get; set; }
}
public class Device
{
public string #id { get; set; }
public string uuid { get; set; }
public string principal { get; set; }
public int blockReason { get; set; }
public int clientId { get; set; }
public string comment { get; set; }
public int compliance { get; set; }
public int countryCode { get; set; }
public int countryId { get; set; }
public string countryName { get; set; }
public string createdAt { get; set; }
public string currentPhoneNumber { get; set; }
public List<Detail> details { get; set; }
public int deviceCount { get; set; }
public string easLastSyncAttempt { get; set; }
public string easUuid { get; set; }
public string emailAddress { get; set; }
public string emailDomain { get; set; }
public bool employeeOwned { get; set; }
public string homeOperator { get; set; }
public int languageCountryId { get; set; }
public int languageId { get; set; }
public string lastConnectedAt { get; set; }
public string manufacturer { get; set; }
public bool mdmManaged { get; set; }
public int mdmProfileUrlId { get; set; }
public string model { get; set; }
public string name { get; set; }
public bool notifyUser { get; set; }
public string #operator { get; set; }
public int operatorId { get; set; }
public string platform { get; set; }
public string platformType { get; set; }
public int quarantinedStatus { get; set; }
public int regCount { get; set; }
public string regType { get; set; }
public string registeredAt { get; set; }
public string status { get; set; }
public int statusCode { get; set; }
public string userDisplayName { get; set; }
public string userFirstName { get; set; }
public string userLastName { get; set; }
public int userSource { get; set; }
public string userUUID { get; set; }
public int wipeReason { get; set; }
}
}
In my MVC razor view i try to access the data:
#model PostenNorge.Models.JsonObjectModels.listDevicesModel
<b>#Model.messages.message</b>
<br />
<br />
<ul>
#foreach(Device d in Model.devices.device)
{
<li>#d.name - #d.uuid - #d.currentPhoneNumber</li>
}
</ul>
On Device in the foreach i get "The type or namespace name "Device" could not be found".
How can i access the nested class types in my view?
You need to specify the nested name:
#foreach(listDevicesModel.Device d in Model.devices.device)
Or use implicit typing:
#foreach(var d in Model.devices.device)
Personally I'd avoid using nested classes here anyway, unless you really have to. Even if you do really have to, I'd rename the top-level class to follow normal naming conventions, i.e. make it start with a capital letter.
I'm getting the following exception when using this bit of code to deserialize a JSON response from CrunchBase. The weird thing is it only happens to certain pages that are being deserialized even though both the results that work fine and the ones that don't both have empty [], empty"", and null values in key:value pairs. How can I cast or correct my mistake?
Exception gets thrown here:
JsonSerializer serializer = new JsonSerializer();
RootObject ro = JsonConvert.DeserializeObject<RootObject>(response);
The inner exception is:
InnerException:
Message=Could not cast or convert from {null} to System.Int32.
Source=Newtonsoft.Json
Thanks for your eyes in advance!
Update:
as asked for the structure of the root object and the additional objects on that JSON endpoint. These were generated by http://json2csharp.com/ after putting the URL of the JSON endpoint into it.
The JSON is long so here are two example links: this one works without error http://api.crunchbase.com/v/1/company/kiip.js , while this other (and others) throws the exception http://api.crunchbase.com/v/1/company/tata-communications.js
public class Image
{
public List<List<object>> available_sizes { get; set; }
public object attribution { get; set; }
}
public class Person
{
public string first_name { get; set; }
public string last_name { get; set; }
public string permalink { get; set; }
}
public class Relationship
{
public bool is_past { get; set; }
public string title { get; set; }
public Person person { get; set; }
}
public class Provider
{
public string name { get; set; }
public string permalink { get; set; }
}
public class Providership
{
public string title { get; set; }
public bool is_past { get; set; }
public Provider provider { get; set; }
}
public class FinancialOrg
{
public string name { get; set; }
public string permalink { get; set; }
}
public class Person2
{
public string first_name { get; set; }
public string last_name { get; set; }
public string permalink { get; set; }
}
public class Investment
{
public object company { get; set; }
public FinancialOrg financial_org { get; set; }
public Person2 person { get; set; }
}
public class FundingRound
{
public string round_code { get; set; }
public string source_url { get; set; }
public string source_description { get; set; }
public double raised_amount { get; set; }
public string raised_currency_code { get; set; }
public int funded_year { get; set; }
public int funded_month { get; set; }
public int funded_day { get; set; }
public List<Investment> investments { get; set; }
}
public class Office
{
public string description { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string zip_code { get; set; }
public string city { get; set; }
public string state_code { get; set; }
public string country_code { get; set; }
public object latitude { get; set; }
public object longitude { get; set; }
}
public class VideoEmbed
{
public string embed_code { get; set; }
public string description { get; set; }
}
public class Screenshot
{
public List<List<object>> available_sizes { get; set; }
public object attribution { get; set; }
}
public class RootObject
{
public string name { get; set; }
public string permalink { get; set; }
public string crunchbase_url { get; set; }
public string homepage_url { get; set; }
public string blog_url { get; set; }
public string blog_feed_url { get; set; }
public string twitter_username { get; set; }
public string category_code { get; set; }
public int number_of_employees { get; set; }
public int founded_year { get; set; }
public int founded_month { get; set; }
public object founded_day { get; set; }
public object deadpooled_year { get; set; }
public object deadpooled_month { get; set; }
public object deadpooled_day { get; set; }
public object deadpooled_url { get; set; }
public string tag_list { get; set; }
public string alias_list { get; set; }
public string email_address { get; set; }
public string phone_number { get; set; }
public string description { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string overview { get; set; }
public Image image { get; set; }
public List<object> products { get; set; }
public List<Relationship> relationships { get; set; }
public List<object> competitions { get; set; }
public List<Providership> providerships { get; set; }
public string total_money_raised { get; set; }
public List<FundingRound> funding_rounds { get; set; }
public List<object> investments { get; set; }
public object acquisition { get; set; }
public List<object> acquisitions { get; set; }
public List<Office> offices { get; set; }
public List<object> milestones { get; set; }
public object ipo { get; set; }
public List<VideoEmbed> video_embeds { get; set; }
public List<Screenshot> screenshots { get; set; }
public List<object> external_links { get; set; }
}
Json.NET supports JSON Schema. You could create a schema with all the required properties marked and validate incoming JSON against it before deserializing. In this you can check if value is null you can make change it to some default value.
Hope this works for you.