我想要获取对象的键,例如Javasript方法Object.keys。这是我的对象:
public object OjbectExample { get; set; }
ObjectExample is:
{"en":"Some Value", "fr":"Value is here"}
在JS中,我可以轻松地获取对象的键,但是在C#中,我不知道这样做。
我想将其转换为字符串:“ $ [en |某些值] [fr |值在这里]”对此有任何想法吗?
Newton
是你的朋友,
static void Main(string[] args)
{
string test = "{'en':'Some Value', 'fr':'Value is here'}"; // this is your json input
ObjectTest converted = JsonConvert.DeserializeObject<ObjectTest>(test); // deserialize json input to your custom object
}
这是示例对象;
public class ObjectTest {
public string en { get; set; }
public string fr { get; set; }
}
或者,你可以采取Keys
并Values
从JObject
这个样子,
static void Main(string[] args)
{
string test = "{'en':'Some Value', 'fr':'Value is here'}"; // this is your json input
JObject converted = JsonConvert.DeserializeObject<JObject>(test);
if (converted != null)
{
Dictionary<string, string> keyValueMap = new Dictionary<string, string>();
foreach (KeyValuePair<string, JToken> keyValuePair in converted)
{
keyValueMap.Add(keyValuePair.Key, keyValuePair.Value.ToString());
}
}
}
结果;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句