I am fetching some data from external Webservice and parsing it to json using Newtonsoft.Json.Linq
我正在从外部Webservice获取一些数据,并使用Newtonsoft.Json.Linq将其解析为json
like this
像这样
JObject o = JObject.Parse(json);
JArray sizes = (JArray) o["data"];
Now the Sizes
looks like this
大小是这样的。
{
[
{
"post_id": "13334556777742_6456",
"message": "messagecomes her",
"attachment": {
"media": [
{
"href": "http://onurl.html",
"alt": "",
"type": "link",
"src": "http://myurl.jpg"
}
],
"name": "come to my name",
"href": "http://mydeeplink.html",
"description": "",
"properties": [],
},
}
]
}
I need to get "src": "http://myurl.jpg"
element from this Json array. I have tried:
我需要从这个Json数组中获得“src”:“http://myurl.jpg”元素。我有尝试:
foreach (JObject obj in sizes)
{
JArray media = (JArray)obj["attachment"];
foreach (JObject obj1 in media)
{
var src = obj1["src"];
}
}
But it's throwing an error:
但它犯了一个错误:
Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'.
at this line
在这条线
JArray media = (JArray)obj["attachment"];
Can any one give me a hand on this?
有人能帮我一下吗?
2 个解决方案
#1
7
Try fix line
尝试修复线
JArray media = (JArray)(obj["attachment"]);
to
来
JArray media = (JArray)(obj["attachment"]["media"]);
#2
1
This is how I handled a scenario that sounds just like yours:
这就是我如何处理一个听起来和你一样的场景:
public static IList<Entity> DeserializeJson(JToken inputObject)
{
IList<Entity> deserializedObject = new List<Entity>();
foreach (JToken iListValue in (JArray)inputObject["root"])
{
Entity entity = new Entity();
entity.DeserializeJson(iListValue);
deserializedObject.Add(entity);
}
return deserializedObject;
}
public virtual void DeserializeJson(JToken inputObject)
{
if (inputObject == null || inputObject.Type == JTokenType.Null)
{
return;
}
inputObject = inputObject["entity"];
JToken assertions = inputObject["assertions"];
if (assertionsValue != null && assertionsValue.Type != JTokenType.Null)
{
Assertions assertions = new Assertions();
assertions.DeserializeJson(assertionsValue);
this.Assertions = assertions;
}
// Continue Parsing
}
#1
7
Try fix line
尝试修复线
JArray media = (JArray)(obj["attachment"]);
to
来
JArray media = (JArray)(obj["attachment"]["media"]);
#2
1
This is how I handled a scenario that sounds just like yours:
这就是我如何处理一个听起来和你一样的场景:
public static IList<Entity> DeserializeJson(JToken inputObject)
{
IList<Entity> deserializedObject = new List<Entity>();
foreach (JToken iListValue in (JArray)inputObject["root"])
{
Entity entity = new Entity();
entity.DeserializeJson(iListValue);
deserializedObject.Add(entity);
}
return deserializedObject;
}
public virtual void DeserializeJson(JToken inputObject)
{
if (inputObject == null || inputObject.Type == JTokenType.Null)
{
return;
}
inputObject = inputObject["entity"];
JToken assertions = inputObject["assertions"];
if (assertionsValue != null && assertionsValue.Type != JTokenType.Null)
{
Assertions assertions = new Assertions();
assertions.DeserializeJson(assertionsValue);
this.Assertions = assertions;
}
// Continue Parsing
}