如何访问反序列化的对象? [英] How to access deserialized objects?

查看:54
本文介绍了如何访问反序列化的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从URL下载了Json String。并创建了反序列化的类使用NewtonJSoft。现在我需要访问反序列化的对象。怎么做?



我的JSON文件:



[ ^ ]



我尝试过:



I have downloaded Json String from URL. and created classes to deserialize Using NewtonJSoft. Now i need to access deserialized objects. How to do this?

My JSON file:

[^]

What I have tried:

public class Rootobject
      {
          public Status status { get; set; }
      }

      public class Status
      {
          public Success[] success { get; set; }
      }

      public class Success
      {
          public User User { get; set; }
          public Speciality Speciality { get; set; }
      }


      public class User
      {
          public string id { get; set; }
          public string username { get; set; }
      }



//反序列化代码


//Deserialize code

var tmp = JsonConvert.DeserializeObject<Rootobject>(json);



我的问题是如何访问用户对象?


My Question is how to access User objects?

推荐答案

本文介绍了这个主题非常详细:在C#中使用JSON& VB [ ^ ]



使用 JSON Utils生成的类:生成C#,VB。来自JSON的网络,SQL表,Java和PHP [ ^ ]:

This article covers the subject in great detail: Working with JSON in C# & VB[^]

Classes generated by using JSON Utils: Generate C#, VB.Net, SQL Table, Java and PHP from JSON[^]:
public class User
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("username")]
    public string Username { get; set; }

    [JsonProperty("username_url")]
    public string UsernameUrl { get; set; }

    [JsonProperty("firstname")]
    public object Firstname { get; set; }

    [JsonProperty("lastname")]
    public object Lastname { get; set; }

    [JsonProperty("email")]
    public string Email { get; set; }

    [JsonProperty("password")]
    public string Password { get; set; }

    [JsonProperty("user_level")]
    public string UserLevel { get; set; }

    [JsonProperty("refer_key")]
    public string ReferKey { get; set; }

    [JsonProperty("image")]
    public string Image { get; set; }

    [JsonProperty("start_time")]
    public object StartTime { get; set; }

    [JsonProperty("end_time")]
    public object EndTime { get; set; }

    [JsonProperty("gender")]
    public string Gender { get; set; }

    [JsonProperty("birthday")]
    public string Birthday { get; set; }

    [JsonProperty("city")]
    public string City { get; set; }

    [JsonProperty("state")]
    public object State { get; set; }

    [JsonProperty("address")]
    public string Address { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("speciality_id")]
    public string SpecialityId { get; set; }

    [JsonProperty("busy_message")]
    public object BusyMessage { get; set; }

    [JsonProperty("created")]
    public string Created { get; set; }

    [JsonProperty("modified")]
    public string Modified { get; set; }

    [JsonProperty("area_expertise")]
    public string AreaExpertise { get; set; }

    [JsonProperty("medical_registration_no")]
    public string MedicalRegistrationNo { get; set; }

    [JsonProperty("additional_services")]
    public string AdditionalServices { get; set; }

    [JsonProperty("qualification")]
    public string Qualification { get; set; }

    [JsonProperty("pa_contact_no")]
    public string PaContactNo { get; set; }

    [JsonProperty("languages_known")]
    public string LanguagesKnown { get; set; }

    [JsonProperty("alter_contact_no")]
    public string AlterContactNo { get; set; }

    [JsonProperty("hospital_address")]
    public string HospitalAddress { get; set; }

    [JsonProperty("available_time")]
    public string AvailableTime { get; set; }

    [JsonProperty("medical_council")]
    public string MedicalCouncil { get; set; }

    [JsonProperty("contact_person")]
    public string ContactPerson { get; set; }

    [JsonProperty("prefereble_fees")]
    public object PreferebleFees { get; set; }

    [JsonProperty("other_service_list")]
    public string OtherServiceList { get; set; }

    [JsonProperty("emergency_service")]
    public string EmergencyService { get; set; }

    [JsonProperty("other_service")]
    public string OtherService { get; set; }

    [JsonProperty("prefer_mode")]
    public string PreferMode { get; set; }

    [JsonProperty("contact_no")]
    public string ContactNo { get; set; }

    [JsonProperty("certificates")]
    public string Certificates { get; set; }

    [JsonProperty("doctor_contact_no")]
    public string DoctorContactNo { get; set; }

    [JsonProperty("website")]
    public string Website { get; set; }

    [JsonProperty("signature")]
    public string Signature { get; set; }

    [JsonProperty("status")]
    public string Status { get; set; }

    [JsonProperty("call_status")]
    public string CallStatus { get; set; }

    [JsonProperty("chat_status")]
    public string ChatStatus { get; set; }

    [JsonProperty("online_status")]
    public string OnlineStatus { get; set; }

    [JsonProperty("ios_UDID")]
    public string IosUDID { get; set; }

    [JsonProperty("ios_badge")]
    public string IosBadge { get; set; }

    [JsonProperty("android_UDID")]
    public object AndroidUDID { get; set; }

    [JsonProperty("last_visit")]
    public object LastVisit { get; set; }

    [JsonProperty("from_app")]
    public string FromApp { get; set; }

    [JsonProperty("for_call")]
    public object ForCall { get; set; }

    [JsonProperty("doctors_type")]
    public string DoctorsType { get; set; }

    [JsonProperty("call_price")]
    public string CallPrice { get; set; }

    [JsonProperty("chat_price")]
    public string ChatPrice { get; set; }

    [JsonProperty("mail_price")]
    public string MailPrice { get; set; }

    [JsonProperty("week_timing")]
    public string WeekTiming { get; set; }
}

public class Speciality
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("slug")]
    public string Slug { get; set; }

    [JsonProperty("image")]
    public string Image { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("status")]
    public string Status { get; set; }

    [JsonProperty("created")]
    public string Created { get; set; }

    [JsonProperty("modified")]
    public string Modified { get; set; }
}

public class Success
{
    [JsonProperty("User")]
    public User User { get; set; }

    [JsonProperty("Speciality")]
    public Speciality Speciality { get; set; }
}

public class Status
{
    [JsonProperty("success")]
    public IList<Success> Success { get; set; }
}

public class Response
{
    [JsonProperty("status")]
    public Status Status { get; set; }
}


RootObject有一个Status属性,包含一系列Success项,所以可能有多个,我们看不到JSON。假设至少有一个,那么下面将使用第一个



RootObject has a Status property with an array of Success items, so there could be more than one, we can't see the JSON. Assuming there is at least one then the below will use the first one

tmp.Status.Success[0].User





如果你想访问所有这些,那么你就可以绕过所有的Success对象。



以上只有在JSON有效并填充时才会起作用,你还没有发布JSON所以我们不知道是不是,所以说上面的代码不起作用没有给任何人足够的信息来帮助你。



If you wanted to access all of them then you'd do a loop around all Success objects.

The above is only going to work if the JSON is valid and populated, you haven't posted the JSON so we don't know if it is, so saying the above code "doesn't work" doesn't give anyone enough information to help you.


这篇关于如何访问反序列化的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆