How to deserialize json body with "0" in parameter - c#

I'm using a API that give me this json as a response,
This is json response body :
{
"meta":{
"status":200,
"message":"success"
},
"data":{
"0":{
"MsgID":"2661689817",
"Status":"6",
"SendTime":"2021-10-3114:30:04",
"DeliverTime":"2021-10-31 14:30:07"
}
}
}
My problem is "0":{... in this body.
How can I deserialize this into a Class.
it can't deserialize on "string _0" prop.

As the #TheGeneral says, this is like a dictionary. You can parse - like this:
public void ParseObject()
{
var response = #"{
'meta':{
'status':200,
'message':'success'
},
'data':{
'0':{
'MsgID':'2661689817',
'Status':'6',
'SendTime':'2021-10-3114:30:04',
'DeliverTime':'2021-10-31 14:30:07'
}
}
}";
var responseObj = JsonConvert.DeserializeObject<MetaData>(response);
}
public class MetaData
{
public Meta Meta { get; set; }
public Dictionary<int, Data> Data { get; set; }
}
public class Meta
{
public string Status { get; set; }
public string Message { get; set; }
}
public class Data
{
public string MsgId { get; set; }
public string Status { get; set; }
public string SendTime { get; set; }
}

try using JsonProperty
public class Data
{
[JsonProperty("0") ]
public _0 _0 { get; set; }
}
public class _0
{
public string MsgID { get; set; }
public string Status { get; set; }
public string SendTime { get; set; }
public string DeliverTime { get; set; }
}

Related

Parse a json generated by jstree in C#

I'm currently receiving the following string of an array that was generated from JsTree:
"[
{
"id":"j1_1",
"text":"Root node 1",
"icon":true,
"li_attr":{
"id":"j1_1"
},
"a_attr":{
"href":"#",
"id":"j1_1_anchor"
},
"data":{
},
"children":[
{
"id":"j1_2",
"text":"Child 1",
"icon":true,
"li_attr":{
"id":"j1_2"
},
"a_attr":{
"href":"#",
"id":"j1_2_anchor"
},
"data":{
},
"children":[
]
}
]
}
]"
I'm trying to deserialize the following item by using
JsTreeModel aaa = Newtonsoft.Json.JsonConvert.DeserializeObject<JsTreeModel>(json);
With JsTreeModel being this class:
public class JsTreeModel
{
[JsonProperty("id")]
public string id { get; set; }
[JsonProperty("text")]
public string text { get; set; }
[JsonProperty("icon")]
public string icon { get; set; }
[JsonProperty("li_attr")]
public string li_attr { get; set; }
[JsonProperty("a_attr")]
public string a_attr { get; set; }
[JsonProperty("data")]
public string data { get; set; }
}
But I'm getting the error`:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'AccesosWeb.Entidad.JsTree.JsTreeModel' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
I think the issue is with how the class is structured, but I don't know how to build it.
This is the model you need in consideration of the json you paste:
You can use the main object:
JsTreeModel myDeserializedClass = JsonConvert.DeserializeObject<JsTreeModel>(json);
and the object is:
public class JsTreeModel
{
public List<MyArray> MyArray { get; set; }
}
public class LiAttr
{
public string id { get; set; }
}
public class AAttr
{
public string href { get; set; }
public string id { get; set; }
}
public class Data
{
}
public class Child
{
public string id { get; set; }
public string text { get; set; }
public bool icon { get; set; }
public LiAttr li_attr { get; set; }
public AAttr a_attr { get; set; }
public Data data { get; set; }
public List<object> children { get; set; }
}
public class MyArray
{
public string id { get; set; }
public string text { get; set; }
public bool icon { get; set; }
public LiAttr li_attr { get; set; }
public AAttr a_attr { get; set; }
public Data data { get; set; }
public List<Child> children { get; set; }
}

Converting json to class object

And I have my object definition as:
public class sendSmsResult
{
public sendSmsResult()
{
MessageIdList=new List<MessageId>();
}
public string ErrorCode { get; set; }
public string PacketId { get; set; }
public List<MessageId> MessageIdList { get; set; }
}
public class MessageId
{
public MessageId()
{
messageid=new List<string>();
}
public List<string> messageid { get; set; }
}
After executing this:
IRestResponse response = client.Execute(request);
I am getting the jsonresult from "response.Content" as
"{\"sendSmsResult\":{\"ErrorCode\":\"0\",\"PacketId\":\"261633886\",\"MessageIdList\":{\"MessageId\":[\"7096779206\",\"-19\"]}}}"
after getting json string I am running this:
JsonConvert.DeserializeObject<sendSmsResult>(response.Content);
gives an error says:
{"Message":"An error has occurred.","ExceptionMessage":"Error
converting value
"{"sendSmsResult":{"ErrorCode":"0","PacketId":"261638899","MessageIdList":{"MessageId":["7097068828","-19"]}}}"
to type 'UME.WebApi.Models.sendSmsResult'. Path '', line 1, position
you need wrapper for sendSmsResult, like bellow:
class sendSmsResultWrapper {
public sendSmsResult sendSmsResult {get;set;}
}
And, typeof sendSmsResult.MessageIdList is MessageId, not List<MessageId>!!
For what it's worth json2csharp.com (no affiliation) suggests the following class structure for your sample data:
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class MessageIdList {
public List<string> MessageId { get; set; }
}
public class SendSmsResult {
public string ErrorCode { get; set; }
public string PacketId { get; set; }
public MessageIdList MessageIdList { get; set; }
}
public class Root {
public SendSmsResult sendSmsResult { get; set; }
}

deserializing JSON to .net object

I'm having problems deserializing JSON to objects. I read a couple of answers but none of them helped me.
This is my JSON file :
{
"meta": {
"status": 200,
"msg": "OK"
},
"response": {
"blog": {
"title": "title",
"name": "name",
"url": "url"
}
}
}
And this is my class :
public class TextInfo
{
public class Meta
{
public int status { get; set; }
public string msg { get; set; }
}
public class Blog
{
public string title { get; set; }
public string name { get; set; }
public string url { get; set; }
}
public class Response
{
public Blog blog { get; set; }
}
public class RootObject
{
public Meta meta { get; set; }
public Response response { get; set; }
}
}
Now, I've tried to deserialize the json like so, but i don't know how to manipulate the data :
TextInfo txt = JsonConvert.DeserializeObject<TextInfo>(json);
Can you help me figure this out ? Thanks in advance.
Three things:
can you provide an error message/log,
do you parse just one item or multiple items? In second case: var txt = JsonConvert.DeserializeObject<List<TextInfo>>(JSONstr);
Try the following:
public class Meta
{
public int status { get; set; }
public string msg { get; set; }
}
public class Response
{
public Blog blog { get; set; }
}
public class Blog
{
public string title { get; set; }
public string name { get; set; }
public string url { get; set; }
}
public class TextInfo
{
public Meta meta { get; set; }
public Response response { get; set; }
}
The only difference is that TextInfo should contain Meta and Response:
public class TextInfo
{
public Meta meta { get; set; }
public Response response { get; set; }
}
public class Meta
{
public int status { get; set; }
public string msg { get; set; }
}
public class Blog
{
public string title { get; set; }
public string name { get; set; }
public string url { get; set; }
}
public class Response
{
public Blog blog { get; set; }
}
And you deserialize like you said:
TextInfo txt = JsonConvert.DeserializeObject<TextInfo>(json);
Your TextInfo class does not have any properties inside it to deserialize. It only declares another classes.
According to your source, you should deserialize into RootObject. This will return you an instance with meta and response properties.
Then you'll access them:
RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine(obj.meta.msg);

Json Naming issue

I am New to JSON and API
I am trying to get some data from an API for which I have Json and want to make a class for it.when I convert it using josn2csharp I get following code. Please let me know what sholud be the proper format of my class so that it converts into json.
public class DataSet {
public string __invalid_name__-i:nil { get; set; } }
public class GenericObject {
public string __invalid_name__-xmlns:d2p1 { get; set; }
public string __invalid_name__-i:type { get; set; }
public string __invalid_name__#text { get; set; } }
public class Message {
public string __invalid_name__-i:nil { get; set; } }
public class FunctionResponse {
public string __invalid_name__-xmlns:i { get; set; }
public string __invalid_name__-xmlns { get; set; }
public DataSet DataSet { get; set; }
public GenericObject GenericObject { get; set; }
public Message Message { get; set; }
public string Success { get; set; } }
public class RootObject {
public FunctionResponse FunctionResponse { get; set; } }
use this question as reference in this I came to know how to write for
public string invalid_name#text { get; set; }
but what about others.
please help
edit 1:
{
   "DataSet": {
      "#nil": "true"
   },
   "GenericObject": {
      "#type": "d2p1:boolean",
      "#text": "true"
   },
   "Message": {
      "#nil": "true"
   },
   "Success": "true"
}
Example:
{
"json":
{
"#thekeyinsidejsonproperty": "value"
}
}
you shoud have
public class RootObject
{
[JsonProperty("json")]
public Json jsonobj;
}
public class Json
{
[JsonProperty("#thekeyinsidejsonproperty")]
public string somepropertyname{ get; set; }
}
hope it helps! :)

JSON deserializing to C# types

I have got following JSON that should be deserialized to C# class:
{"status":"error","messages":[{"level":"error","key":"InvalidTokenError","dsc":"Invalid
token"}]}
So I have a questions what is the kind of type should be
[{"level":"error","key":"InvalidTokenError","dsc":"Invalid token"}]
?
Is this array, list or class?
You can use json2csharp.com to get the types of the JSON.
JSON:
{"status":"error","messages":[{"level":"error","key":"InvalidTokenError","dsc":"Invalid token"}]}
Here is the classes generated for your JSON:
public class Message
{
public string level { get; set; }
public string key { get; set; }
public string dsc { get; set; }
}
public class RootObject
{
public string status { get; set; }
public List<Message> messages { get; set; }
}
for JSON:
[{"level":"error","key":"InvalidTokenError","dsc":"Invalid token"}]
Type:
public class Message
{
public string level { get; set; }
public string key { get; set; }
public string dsc { get; set; }
}
You can use this site
public class Message
{
public string level { get; set; }
public string key { get; set; }
public string dsc { get; set; }
}
public class RootObject
{
public string status { get; set; }
public List<Message> messages { get; set; }
}

Categories