Reputation: 97
I'm working with REST services from Oracle database, and I'm trying to cast the json response into my class model, but I can't get it, this is the type of the JSON
{
"items": [
{
"id": 2,
"role_id": 1,
"local_id": 1,
"nombre": "test",
"apellido": "test",
"email": "test",
"phone_number": "123123",
"rut": "1",
"password": "1231212",
"token": "C0C7C76D30BD3DCAEFC96F40275BDC0A"
},
{
"id": 1,
"role_id": 6,
"local_id": 1,
"nombre": "Cristobal",
"apellido": "Gonzalez",
"email": "[email protected]",
"phone_number": "22222",
"rut": "2-1",
"password": "actualizada",
"token": "34173CB38F07F89DDBEBC2AC9128303F"
}
],
"hasMore": false,
"limit": 25,
"offset": 0,
"count": 2,
"links": [
{
"rel": "self",
"href": "https://jl0ax7eh9vmstei-portafolio2020.adb.sa-saopaulo-1.oraclecloudapps.com/ords/admin/usuario/user"
},
{
"rel": "describedby",
"href": "https://jl0ax7eh9vmstei-portafolio2020.adb.sa-saopaulo-1.oraclecloudapps.com/ords/admin/metadata-catalog/usuario/item"
},
{
"rel": "first",
"href": "https://jl0ax7eh9vmstei-portafolio2020.adb.sa-saopaulo-1.oraclecloudapps.com/ords/admin/usuario/user"
}
]
}
I just need to work with the items part, and my class is like
public int id_usuario { get; set; }
public int id_rol { get; set; }
public int id_local { get; set; }
public string pnombre { get; set; }
public string papellido { get; set; }
public string correo { get; set; }
public string phone { get; set; }
public string rut { get; set; }
public string pass { get; set; }
public string token { get; set; }
So my trouble is that when I try to Deserialize it, I can't parse into my class, I supposed that is for the "extra stuff" that I don't need... This is my method to convert into my class type
string strresult_test = null;
using (Stream stream = responseObjGet.GetResponseStream())
{
StreamReader sr = new StreamReader(stream);
strresult_test = sr.ReadToEnd();
strresult_test.ToString();
Response.Write(sr);
sr.Close();
}
var serial = new JavaScriptSerializer();
ItemUsuario testv2 = JsonConvert.DeserializeObject<ItemUsuario>(strresult_test);
Response.Write("g");
List<ItemUsuario> salida = serial.Deserialize<List<ItemUsuario>>(strresult_test);
When I get the "testv2" variable, is null... What else can I do?
Thanks for the help!
Upvotes: 1
Views: 1163
Reputation: 11364
If you want to deserialize that json into Just the class you are using, you will first need to get to the items
object in the json and then deserialize it to your class. You can leverage JObject.Parse
in combination with JsonConvert.DeserializeObject<YourClass>
This is a sample code that can be used to take that json into your class object.
var jsonObject = JObject.Parse(stresult_test);
List<ItemUsuario> testv2 = JsonConvert.DeserializeObject<List<ItemUsuario>>(jsonObject["items"].ToString());
jsonObject["items"]
is an array / list so make sure you deserialize it to a List of your class.. not just the class itself.
As other posts suggests, and based on coding standards, use [JsonProperty("name")]
in your class and make sure that the property Name matches the one in json.. otherwise you will get null
for that property. in C#, the property names should start with a capital letter as well.
public class ItemUsuario {
[JsonString("id")]
public int Id {get;set}
[JsonProperty("role_id")]
public int RoleID {get;set;}
...
Upvotes: 0
Reputation: 33387
Your model is wrong.
This is the correct way of doing it:
public partial class ItemUsuario
{
[JsonProperty("items")]
public Item[] Items { get; set; }
[JsonProperty("hasMore")]
public bool HasMore { get; set; }
[JsonProperty("limit")]
public long Limit { get; set; }
[JsonProperty("offset")]
public long Offset { get; set; }
[JsonProperty("count")]
public long Count { get; set; }
[JsonProperty("links")]
public Link[] Links { get; set; }
}
public partial class Item
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("role_id")]
public long RoleId { get; set; }
[JsonProperty("local_id")]
public long LocalId { get; set; }
[JsonProperty("nombre")]
public string Nombre { get; set; }
[JsonProperty("apellido")]
public string Apellido { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("phone_number")]
public string PhoneNumber { get; set; }
[JsonProperty("rut")]
public string Rut { get; set; }
[JsonProperty("password")]
public string Password { get; set; }
[JsonProperty("token")]
public string Token { get; set; }
}
public partial class Link
{
[JsonProperty("rel")]
public string Rel { get; set; }
[JsonProperty("href")]
public Uri Href { get; set; }
}
I generated this code using this tool. I have tested it and it works.
Upvotes: 2
Reputation: 141600
Your ItemUsuario
array is actually value of property "items":
public class ItemContainer
{
[JsonProperty("items")]
public ItemUsuario[] Items { get; set; }
}
public class ItemUsuario
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("role_id")]
public long RoleId { get; set; }
[JsonProperty("local_id")]
public long LocalId { get; set; }
.....
}
var result = JsonConvert.DeserializeObject<ItemContainer>(strresult_test);
Upvotes: 1
Reputation: 1479
For the Deserialization to work make sure that the variable names in class matches with the names in the json. If you want to retain both as same, do consider decorating with the Attribute like
public class ItemUsuario
{
//.. Line omitted for beveity
[JsonProperty("role_id")]
public string id_rol { get; set; }
...
}
Upvotes: 0