I have a a api response from Azure Ml which returns the follow json bellow:
responseApi content
{ "Results": { "id_unidade_negocio": [ { "msgSucesso": "Processamento concluĂdo com sucesso" } ] } }
I would like to extract only the "msgSucesso": "Processamento concluĂdo com sucesso"
I try the code below, however didn't work.
var obj = JObject.Parse(responseApi);
var msg = (string)obj.SelectToken("Results.msgSucesso");
I didin't try to deserialize the json to a object class, because I don't know the right format class to create it to be compatible with output json.
Whats is the best way to extract this info from json response?
Or How can I create a class that fit in this json output in other to convert the json to object?
https://stackoverflow.com/a/24233127/2906166
check above link
public class IdUnidadeNegocio
{
public string msgSucesso { get; set; }
}
public class Results
{
public List<IdUnidadeNegocio> id_unidade_negocio { get; set; }
}
public class RootObject
{
public Results Results { get; set; }
}
var obj = JsonConvert.DeserializeObject<RootObject>(responseApi);
Console.WriteLine(obj.Results.First().msgSucesso);
I am aware this topic has been previously posted. But I tried following them. However, my result is still not being shown. I would appreciate any help possible. Thanks in advance. :) I am getting the following error: System.NullReferenceException: 'Object reference not set to an instance of an object.'
I am trying to deserialize a JSON object into a c# object to output the property score.
My Json output from json = toneAnalyzer.Tone(toneInput) :
{
"document_tone" : {
"tones" :
[
{
"score" : 0.70123,
"tone_id" : "tentative",
"tone_name" : "Tentative"
}
]
}
}
I have carried out the following code:
var json = toneAnalyzer.Tone(toneInput); // this is my json source
DocTone myResult = new DocTone();
myResult = JsonConvert.DeserializeObject<DocTone>(json.Response);
foreach (var myTone in myResult.tones)
{
Console.Write(myTone.Score);
Console.ReadKey();
}
// Console.WriteLine(myResult);
// Console.WriteLine(result.Response);
}
public class MyTone1
{
[JsonProperty("score")]
public double Score { get; set; }
[JsonProperty("tone_id")]
public string Tone_Id { get; set; }
[JsonProperty("tone_name")]
public string Tone_Name { get; set; }
}
public class DocTone
{
[JsonProperty("tones")]
public List<MyTone1> tones { get; set; }
}
You've got a slight mistake with the object you are deserialising to.
Your root object is not DocTone, but actually the object that has a property containing the DocTone object (via the document_tone element).
Define a root object (call it whatever you like) and then deserialise to that:
public class RootObject
{
[JsonProperty("document_tone")]
public DocTone DocTone { get; set; }
}
Deserialise and then access via the DocTone property:
RootObject myResult;
myResult = JsonConvert.DeserializeObject<RootObject>(json.Response);
foreach (var myTone in myResult.DocTone.tones)
...
The reason that you are experiencing the NullReferencException is because when you deserialise to a DocTone object, the tones property is NULL.
I have pretty big json string and I am trying to read into object in azure functions only small part of it and I am not sure how to do it.
Link to sample json https://learn.microsoft.com/en-us/azure/devops/service-hooks/events?view=azure-devops#workitem.updated
I tried to create class but I dont really know how to access "WorkItemType" from json because it looks nested into "Fields".
My code of object
public class jsonObject
public string System.WorkItemType { get; set; }
public string System.State { get; set; }
}
Deserializing here
dynamic eventData = await req.Content.ReadAsAsync<jsonObject>();
Try this:
public class jsonObject
public string WorkItemType { get; set; }
public string State { get; set; }
}
Deserialization:
jsonObject eventData = await req.Content.ReadAsAsync<jsonObject>();
eventData object contains your data
i am a newbie to coding , I have a nested json coming from a external source the json format is looks something like this ..
{
"data":"data",
"data":
{
"data":"data",
//----------------e.t.c the internal objects could be in n number
}
}
so how do i deserialize the json object and use the data as a list in order to do my other operations like sql and posting the data??
You need to create a class that is appropriate with the Json, like that:
public someClass
{
public string data1 { get; set; }
public DataClass dataArr { get; set; }
}
public DataClass
{
public string insideData { get; set; }
}
after you do this you need the following code:
var jsonDto = JsonConvert.DeserializeObject<someClass>(yourJson);
I am getting following JSON data
[{"id":"1","text":"System Admin","target":{"jQuery1710835279177001846":12},"checked":true,"state":"open"},
{"id":"2","text":"HRMS","target":{"jQuery1710835279177001846":34},"checked":false,"state":"open"},
{"id":"3","text":"SDBMS","target":{"jQuery1710835279177001846":42},"checked":false},
{"id":"8","text":"Admin","target":{"jQuery1710835279177001846":43},"checked":false},
{"id":"9","text":"My test Admin","target":{"jQuery1710835279177001846":44},"checked":false,"state":"open"},
{"id":"24","text":"ModuleName","target":{"jQuery1710835279177001846":46},"checked":false,"state":"open"}]
which try to parsed using Json.Net using strongly type
this are my property class
public class testclass
{
public string id { get; set; }
public string text { get; set; }
public string #checked { get; set; }
public string state { get; set; }
public target jQuery1710835279177001846 { get; set; }
}
public class testclass2
{
public List<testclass> testclass1 { get; set; }
}
public class target
{
public string jQuery1710835279177001846 { get; set; }
}
and here i am trying to access the data i am getting exception
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'QuexstERP.Web.UI.Areas.SysAdmin.Controllers.testclass' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
My controller code look like
public void Test(string Name, object modeldata)
{
var obj = JsonConvert.DeserializeObject<testclass>(Name);
}
Any idea how to solve this issue in C#
Your Json string looks to have serialized array object in it because it contains [ ]. It means you have a Json string which is formed after serialization of array object. So you need to deserialized into array object, so try this
var obj = JsonConvert.DeserializeObject<List<testclass>>(jsonString);
you have Array of TestClass. so it should be like this.
var model= JsonConvert.DeserializeObject<List<testclass>>(Name);
why you are using JSonConvert ? in MVC3 you can do like this
return Json(yourmodel,JsonRequestBehavior.AllowGet);
Your json objects are like this
{
"id":"1",
"text":"System Admin",
"target":{
"jQuery1710835279177001846":12
},
"checked":true,
"state":"open"
}
It should be like this I guess
{
"id":"1",
"text":"System Admin",
"jQuery1710835279177001846":12,
"checked":true,
"state":"open"
}