C# 通过属性c获取json属性#
我有一个Json对象,如下所示C# 通过属性c获取json属性#,c#,json,C#,Json,我有一个Json对象,如下所示 "log": { "Response": [ { "@type": "Authentication", "Status": "True", "Token": "cc622e9c-0d56-4774-8d79-543c525471b4" }, { "@type": "GetApplication", "AppId": 100, "Available"
"log": {
"Response": [
{
"@type": "Authentication",
"Status": "True",
"Token": "cc622e9c-0d56-4774-8d79-543c525471b4"
},
{
"@type": "GetApplication",
"AppId": 100,
"Available": "True"
}]}
string json = @"{
""log"": {
""Response"": [{
""@type"": ""Authentication"",
""Status"": ""True"",
""Token"": ""cc622e9c-0d56-4774-8d79-543c525471b4""
}, {
""@type"": ""GetApplication"",
""AppId"": 100,
""Available"": ""True""
}]
}
}";
JObject result = JObject.Parse(json);
foreach(var item in result["log"]["Response"])
{
Console.WriteLine(item["@type"]);
Console.WriteLine(item["AppId"]);
}
我需要访问appId属性。我已经尝试了下面的代码,它给出了空引用错误。请帮我找出错误
dynamic JsonText = JObject.Parse(result);
string AppId= JsonText ["log"]["Response @type='GetApplication'"]["AppId"].Tostring();
string json = @"{
""log"": {
""Response"": [{
""@type"": ""Authentication"",
""Status"": ""True"",
""Token"": ""cc622e9c-0d56-4774-8d79-543c525471b4""
}, {
""@type"": ""GetApplication"",
""AppId"": 100,
""Available"": ""True""
}]
}
}";
JObject result = JObject.Parse(json);
foreach(var item in result["log"]["Response"])
{
Console.WriteLine(item["@type"]);
Console.WriteLine(item["AppId"]);
}
这里
string json = @"{
""log"": {
""Response"": [{
""@type"": ""Authentication"",
""Status"": ""True"",
""Token"": ""cc622e9c-0d56-4774-8d79-543c525471b4""
}, {
""@type"": ""GetApplication"",
""AppId"": 100,
""Available"": ""True""
}]
}
}";
JObject result = JObject.Parse(json);
foreach(var item in result["log"]["Response"])
{
Console.WriteLine(item["@type"]);
Console.WriteLine(item["AppId"]);
}
您不需要使用dynamic,使用JObject
,然后在响应中使用该循环,并使用@type
,您可以使用Newtonsoft.Json和LINQ get id使用并生成模型类,如我所示
string json = @"{
""log"": {
""Response"": [{
""@type"": ""Authentication"",
""Status"": ""True"",
""Token"": ""cc622e9c-0d56-4774-8d79-543c525471b4""
}, {
""@type"": ""GetApplication"",
""AppId"": 100,
""Available"": ""True""
}]
}
}";
JObject result = JObject.Parse(json);
foreach(var item in result["log"]["Response"])
{
Console.WriteLine(item["@type"]);
Console.WriteLine(item["AppId"]);
}
模范班
string json = @"{
""log"": {
""Response"": [{
""@type"": ""Authentication"",
""Status"": ""True"",
""Token"": ""cc622e9c-0d56-4774-8d79-543c525471b4""
}, {
""@type"": ""GetApplication"",
""AppId"": 100,
""Available"": ""True""
}]
}
}";
JObject result = JObject.Parse(json);
foreach(var item in result["log"]["Response"])
{
Console.WriteLine(item["@type"]);
Console.WriteLine(item["AppId"]);
}
public class Response
{ [JsonProperty("@type")]
public string Type { get; set; }
[JsonProperty("Status")]
public string Status { get; set; }
[JsonProperty("Token")]
public string Token { get; set; }
[JsonProperty("AppId")]
public int? AppId { get; set; }
[JsonProperty("Available")]
public string Available { get; set; }
}
public class Log
{
public List<Response> Response { get; set; }
}
public class RootObject
{
public Log log { get; set; }
}
公共类响应
{[JsonProperty(“@type”)]
公共字符串类型{get;set;}
[JsonProperty(“状态”)]
公共字符串状态{get;set;}
[JsonProperty(“令牌”)]
公共字符串标记{get;set;}
[JsonProperty(“AppId”)]
公共int?AppId{get;set;}
[JsonProperty(“可用”)]
可用的公共字符串{get;set;}
}
公共类日志
{
公共列表响应{get;set;}
}
公共类根对象
{
公共日志日志{get;set;}
}
.cs
string json = @"{
""log"": {
""Response"": [{
""@type"": ""Authentication"",
""Status"": ""True"",
""Token"": ""cc622e9c-0d56-4774-8d79-543c525471b4""
}, {
""@type"": ""GetApplication"",
""AppId"": 100,
""Available"": ""True""
}]
}
}";
JObject result = JObject.Parse(json);
foreach(var item in result["log"]["Response"])
{
Console.WriteLine(item["@type"]);
Console.WriteLine(item["AppId"]);
}
var results=JsonConvert.DeserializeObject(json);
var id=results.log.Response.FirstOrDefault(d=>d.Type==“GetApplication”).AppId;
要访问AppId
属性,如示例中所示:
string json = @"{
""log"": {
""Response"": [{
""@type"": ""Authentication"",
""Status"": ""True"",
""Token"": ""cc622e9c-0d56-4774-8d79-543c525471b4""
}, {
""@type"": ""GetApplication"",
""AppId"": 100,
""Available"": ""True""
}]
}
}";
JObject result = JObject.Parse(json);
foreach(var item in result["log"]["Response"])
{
Console.WriteLine(item["@type"]);
Console.WriteLine(item["AppId"]);
}
string AppId = JObject.Parse(result)["log"].SelectToken("$.Response[?(@.@type=='GetApplication')]")["AppId"].ToString();
@user3064309 hi,如果
(“@type”:“GetApplication”)
是第二个且不更改位置。因此,您可以使用obj[“log”][“Response”][1][“AppId”].ToString() 我已经编写了一个扩展函数,它将是深度的第三级。如果需要,可以将其深度设置为通用,因此下面的代码
string json = @"{
""log"": {
""Response"": [{
""@type"": ""Authentication"",
""Status"": ""True"",
""Token"": ""cc622e9c-0d56-4774-8d79-543c525471b4""
}, {
""@type"": ""GetApplication"",
""AppId"": 100,
""Available"": ""True""
}]
}
}";
JObject result = JObject.Parse(json);
foreach(var item in result["log"]["Response"])
{
Console.WriteLine(item["@type"]);
Console.WriteLine(item["AppId"]);
}
public static object GetJsonPropValue(this object obj, params string[] props)
{
try
{
var jsonString = obj.ToString().Replace("=", ":");
var objects = JsonConvert.DeserializeObject<dynamic>(jsonString);
foreach (var o in objects)
{
JObject jo = JObject.Parse("{" + o.ToString().Replace("=", ":") + "}");
if (props.Count() == 1 && jo.SelectToken(props[0]) != null)
return jo.SelectToken(props[0]).ToString();
if (props.Count() == 2 && jo.SelectToken(props[0])?.SelectToken(props[1]) != null)
return jo.SelectToken(props[0])?.SelectToken(props[1]).ToString();
if (props.Count() == 2 && jo.SelectToken(props[0])?.SelectToken(props[1])?.SelectToken(props[2]) != null)
return jo.SelectToken(props[0])?.SelectToken(props[1])?.SelectToken(props[2]).ToString();
}
}
catch (Exception ex)
{
throw new ArgumentException("GetJsonPropValue : " + ex.Message);
}
return null;
}
public静态对象GetJsonPropValue(此对象为obj,参数字符串为[]props)
{
尝试
{
var jsonString=obj.ToString().Replace(“=”,“:”);
var objects=JsonConvert.DeserializeObject(jsonString);
foreach(对象中的var o)
{
JObject jo=JObject.Parse(“{”+o.ToString().Replace(“=”,“:”+“}”);
if(props.Count()==1&&jo.SelectToken(props[0])!=null)
返回jo.SelectToken(props[0]).ToString();
if(props.Count()==2&&jo.SelectToken(props[0])?.SelectToken(props[1])!=null)
返回jo.SelectToken(props[0])?.SelectToken(props[1]).ToString();
if(props.Count()==2&&jo.SelectToken(props[0])?.SelectToken(props[1])?.SelectToken(props[2])!=null
返回jo.SelectToken(props[0])?.SelectToken(props[1])?.SelectToken(props[2]).ToString();
}
}
捕获(例外情况除外)
{
抛出新的ArgumentException(“GetJsonPropValue:+ex.Message”);
}
返回null;
}
我还为普通c#对象编写了一个扩展,动态获取prop值
string json = @"{
""log"": {
""Response"": [{
""@type"": ""Authentication"",
""Status"": ""True"",
""Token"": ""cc622e9c-0d56-4774-8d79-543c525471b4""
}, {
""@type"": ""GetApplication"",
""AppId"": 100,
""Available"": ""True""
}]
}
}";
JObject result = JObject.Parse(json);
foreach(var item in result["log"]["Response"])
{
Console.WriteLine(item["@type"]);
Console.WriteLine(item["AppId"]);
}
public static object GetPropValue(this object obj, Type typeName, string propName)
{
try
{
IList<PropertyInfo> props = new List<PropertyInfo>(typeName.GetProperties());
foreach (PropertyInfo prop in props)
{
if (prop.Name == propName)
{
object propValue = prop.GetValue(obj, null);
return propValue;
}
}
}
catch (Exception ex)
{
throw new ArgumentException("GetPropValue : " + ex.Message);
}
return null;
}
公共静态对象GetPropValue(此对象对象,键入typeName,字符串propName)
{
尝试
{
IList props=新列表(typeName.GetProperties());
foreach(PropertyInfo props in props)
{
如果(prop.Name==propName)
{
object propValue=prop.GetValue(obj,null);
返回值;
}
}
}
捕获(例外情况除外)
{
抛出新的ArgumentException(“GetPropValue:+ex.Message”);
}
返回null;
}
谢谢你。。。让我试试this@user3064309如果有人帮助你,你可以将答案标记为正确。