c# 转化dynamic_C#教程之c# json转换成dynamic对象,然后在dynamic对象
https://www.xin3721.com/eschool/python.htmlusing Newtonsoft.Json;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApp1{cl
https://www.xin3721.com/eschool/python.html
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string ss = TestAA();
Console.WriteLine(ss);
Console.ReadKey();
}
private static string TestAA()
{
string allowAttr = string.Empty;
string json = "{\"UserName\":\"Jack\",\"LoS\":\"Beijing\",\"Group\":\"34\"}";
//此处模拟在不建实体类的情况下,反转将json返回成dynamic对象
var DynamicObject = JsonConvert.DeserializeObject(json);
//var _value = DynamicObject["LoS"];
List ssList = new List();
ssList.Add("LoS");
ssList.Add("Test2");
foreach(string item in ssList)
{
var value2 = DynamicObject[item];//动态获取字符串列表中的字段去dynamic对象中找对应字段的值
if(value2!=null)
{
allowAttr = string.Format("{0}{1}", value2, "_Attribute");
//string allowAttr2 = string.Format("{0}{1}", value2.Value, "_Attribute");
}
}
return allowAttr;
//Console.WriteLine(DynamicObject.LoS);
//Type Ts = DynamicObject.GetType();
//object o = Ts.GetProperty("Name").GetValue(DynamicObject, null);
//string Value = Convert.ToString(o);
//if (string.IsNullOrEmpty(Value))
//{
// return null;
//}
//else
//{
// return Value;
//}
}
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)