VB.net 2010 視頻教程 VB.net 2010 視頻教程 VB.net 2010 視頻教程
SQL Server 2008 視頻教程 c#入門經典教程 Visual Basic從門到精通視頻教程
當前位置:
魔兽世界wow > 編程開發 > C#編程 >
  • C#教程之c# json轉換成dynamic對象,然后在dynamic對象

  • 2019-01-20 21:24 來源:未知

魔兽世界wow www.geyjm.icu 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<dynamic>(json);
//var _value = DynamicObject["LoS"];
List<string> ssList = new List<string>();
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;
//}
}
}
}

相關教程