On my API side, I use JsonConvert.SerializeObject() to set it to a string. This is the output from my API:
{"ContentType":null,"SerializerSettings":null,"StatusCode":null,"Value":{"draw":1,"recordsTotal":0,"recordsFiltered":0,"data":[],"order":null,"orderdir":null}}
This is what the API looks like:
[HttpPost("Preview")]
public JsonResult Preview([FromBody]AnnouncementAccessPreviewRequestViewModel svm)
{
ApiResponseViewModel arvm = new ApiResponseViewModel();
var res = announcementData.Preview(svm.SearchViewModel, svm.TenantId);
arvm.IsSuccessful = true;
arvm.Message = null;
arvm.Output = JsonConvert.SerializeObject(res);
return Json(arvm);
}
arvm.Output is a string
How can I only take the Value section from the output?
CodePudding user response:
That's how I solved it when I had this problem. private YourModel GetJsonObject()
var parsedObject = JObject.Parse(resultContent);
string p = parsedObject.ToString();
if (p.Contains("Succes"))
{
string popupJson = JObject.Parse(parsedObject["data"].ToString()).ToString();
YourModel= JsonConvert.DeserializeObject<YourModel>(popupJson);
return YourModel;
}
There is a side way to get this model from https://json2csharp.com/.
CodePudding user response:
you can try this
var output=JObject.Parse(json);
Value value=output["Value"].ToObject<Value>();
class
public class Value
{
public int draw { get; set; }
public int recordsTotal { get; set; }
public int recordsFiltered { get; set; }
public List<object> data { get; set; }
public object order { get; set; }
public string orderdir { get; set; }
}
or if you can change Api
public JsonResult Preview([FromBody]AnnouncementAccessPreviewRequestViewModel svm)
{
var res = announcementData.Preview(svm.SearchViewModel, svm.TenantId);
var output = JsonConvert.SerializeObject(res);
return Json(output);
}
but the right way is
public IActionResult Preview([FromBody]AnnouncementAccessPreviewRequestViewModel svm)
{
return announcementData.Preview(svm.SearchViewModel, svm.TenantId);
}
in this case you don't need to serialize or deserialize