Get value from JSON response in C#

69 views Asked by At

Need help regarding search or get value in JSON response I try to get value of "id"

"{\"items\":[{\"libId\":\"gc1373wgw1mamn8baegfg2q2tsw4lnv1\",\"id\":\"bya51p40751d6oh0e653d5j645st5abk\",\"createdAt\":\"2023-08-10T13:06:05.985Z\",\"createdBy\":\"f393281c-265e-4116-95fd-804e2c10ef5a\",\"modifiedAt\":\"2023-08-14T12:53:29.645Z\",\"modifiedBy\":\"f393281c-265e-4116-95fd-804e2c10ef5a\",\"v\":2,\"name\":\"Steel Status\",\"schema\":{\"v\":1,\"props\":{\"prop_7jf006f02m19eilzpql7hziuonnajtaj\":{\"type\":\"boolean\",\"required\":true,\"default\":false},\"prop_cvelncsngupkcsczuwpbzpb8wvckvmo9\":{\"type\":\"boolean\",\"required\":true,\"default\":false}}},\"i18n\":{\"__definition-sort-order__\":{\"name\":\"__definition-sort-order__\",\"props\":{\"__definition-sort-order__\":\"0\"}},\"en-US\":{\"name\":\"Steel Status\",\"props\":{\"prop_7jf006f02m19eilzpql7hziuonnajtaj\":\"fabrication\",\"prop_cvelncsngupkcsczuwpbzpb8wvckvmo9\":\"Erection\"}},\"__prop-order__\":{\"name\":\"__prop-order__\",\"props\":{\"prop_7jf006f02m19eilzpql7hziuonnajtaj\":\"0\",\"prop_cvelncsngupkcsczuwpbzpb8wvckvmo9\":\"1\"}}},\"_actions\":[\"pset:GetDefVersionSchema\",\"pset:GetDefVersion\",\"pset:GetDefVersions\",\"pset:GetDef\",\"pset:UpdateDef\",\"pset:ValidatePSets\",\"pset:Subscribe\",\"pset:GetPSet\",\"pset:ListPSets\",\"pset:PutPSet\"]}]}"

I try below solutions but it get null

JObject obj = JObject.Parse(responseBody);
string id = (string)obj["id"];
var datas = (JObject)JsonConvert.DeserializeObject(responseBody);
string nodid = datas["id"].Value<string>();
2

There are 2 answers

1
Qwertiy On BEST ANSWER
obj["items"][0]["id"]

0
scopolamin On

Quick solution:

using System.Text.Json;
    
string json = "{\"items\":[{\"libId\":\"gc1373wgw1mamn8baegfg2q2tsw4lnv1\",\"id\":\"bya51p40751d6oh0e653d5j645st5abk\",\"createdAt\":\"2023-08-10T13:06:05.985Z\",\"createdBy\":\"f393281c-265e-4116-95fd-804e2c10ef5a\",\"modifiedAt\":\"2023-08-14T12:53:29.645Z\",\"modifiedBy\":\"f393281c-265e-4116-95fd-804e2c10ef5a\",\"v\":2,\"name\":\"Steel Status\",\"schema\":{\"v\":1,\"props\":{\"prop_7jf006f02m19eilzpql7hziuonnajtaj\":{\"type\":\"boolean\",\"required\":true,\"default\":false},\"prop_cvelncsngupkcsczuwpbzpb8wvckvmo9\":{\"type\":\"boolean\",\"required\":true,\"default\":false}}},\"i18n\":{\"__definition-sort-order__\":{\"name\":\"__definition-sort-order__\",\"props\":{\"__definition-sort-order__\":\"0\"}},\"en-US\":{\"name\":\"Steel Status\",\"props\":{\"prop_7jf006f02m19eilzpql7hziuonnajtaj\":\"fabrication\",\"prop_cvelncsngupkcsczuwpbzpb8wvckvmo9\":\"Erection\"}},\"__prop-order__\":{\"name\":\"__prop-order__\",\"props\":{\"prop_7jf006f02m19eilzpql7hziuonnajtaj\":\"0\",\"prop_cvelncsngupkcsczuwpbzpb8wvckvmo9\":\"1\"}}},\"_actions\":[\"pset:GetDefVersionSchema\",\"pset:GetDefVersion\",\"pset:GetDefVersions\",\"pset:GetDef\",\"pset:UpdateDef\",\"pset:ValidatePSets\",\"pset:Subscribe\",\"pset:GetPSet\",\"pset:ListPSets\",\"pset:PutPSet\"]}]}";
    
using (JsonDocument document = JsonDocument.Parse(json))
{
    string id = document.RootElement.GetProperty("items")[0].GetProperty("id").GetString();
}