I've converted a XML into a JSON:
我已将XML转换为JSON:
var json = JsonConvert.SerializeXmlNode(doc);
This is the result:
这是结果:
"author": {
"name": "Hey Guappo",
"yt:userId": "asfajgf346346fghsdgsWfiqcfr1pfQ"
}
and I'd like to access to the yt:userId
.
我想访问yt:userId。
I can't do this in .NET:
我不能在.NET中这样做:
dynamic objectParsed = JObject.Parse(json);
var userID= (string)objectParsed.entry.author.yt:userId;
because of :
. So how can I manage Namespace in JSON? I have:
因为 :。那么如何在JSON中管理Namespace呢?我有:
var yt = XNamespace.Get("http://gdata.youtube.com/schemas/2007");
but I don't know how to apply it...
但我不知道如何应用它...
2 个解决方案
#1
3
Try this:
尝试这个:
JObject json = JObject.Parse(json);
string userId = json["author"]["yt:userId"].ToString();
#2
0
This WILL work with the dynamic just fine. You don't have to use a JObject. In addition, I've highlighted the fact that you can use . notation UP TO your "yt:userId", at which point you need to index by string value.
这将适用于动态就好。您不必使用JObject。另外,我强调了你可以使用的事实。符号UP到你的“yt:userId”,此时你需要按字符串值索引。
dynamic objectParsed = JObject.Parse(json);
string userId = json.author["yt:userId"].ToString();
There is really no reason to NOT use a JObject, as Tobberoth pointed out, but there is also no technical restriction on using a dynamic if you prefer.
正如Tobberoth指出的那样,没有理由不使用JObject,但如果您愿意,也没有使用动态的技术限制。
As a side note, avoid using an explicit cast to string with (string). Always use ToString().
作为旁注,请避免使用显式强制转换为字符串(字符串)。始终使用ToString()。
#1
3
Try this:
尝试这个:
JObject json = JObject.Parse(json);
string userId = json["author"]["yt:userId"].ToString();
#2
0
This WILL work with the dynamic just fine. You don't have to use a JObject. In addition, I've highlighted the fact that you can use . notation UP TO your "yt:userId", at which point you need to index by string value.
这将适用于动态就好。您不必使用JObject。另外,我强调了你可以使用的事实。符号UP到你的“yt:userId”,此时你需要按字符串值索引。
dynamic objectParsed = JObject.Parse(json);
string userId = json.author["yt:userId"].ToString();
There is really no reason to NOT use a JObject, as Tobberoth pointed out, but there is also no technical restriction on using a dynamic if you prefer.
正如Tobberoth指出的那样,没有理由不使用JObject,但如果您愿意,也没有使用动态的技术限制。
As a side note, avoid using an explicit cast to string with (string). Always use ToString().
作为旁注,请避免使用显式强制转换为字符串(字符串)。始终使用ToString()。