无法隐式转换类型' Newtonsoft.Json.Linq.JArray []' to' Newtonsoft.Json.Linq.JToken'

时间:2017-08-02 18:15:53

标签: c#

class Resturant
{
  public string Name { get; set; }
  public double Latitude { get; set; }
  public double Longitude { get; set; }
}

JObject feature = new JObject();
Resturant item = new Resturant();

item.Latitude = (double)Responseobj["results"][0]["geometry"]["location"]["lat"];
item.Longitude = (double)Responseobj["results"][0]["geometry"]["location"]["lng"];
feature["geometry"]["coordinates"]  = new JArray[2];
feature["geometry"]["coordinates"][0] = item.Latitude;
feature["geometry"]["coordinates"][1] = item.Longitude;

给我一​​个错误

  

无法隐式转换类型' Newtonsoft.Json.Linq.JArray []' to' Newtonsoft.Json.Linq.JToken'

plz帮助我正确地做到这一点

1 个答案:

答案 0 :(得分:0)

事实证明我正在解决同样的问题。在经过大量研究Stack Overflow之后,我已经解决了这个问题。

你需要的是一个帮助类来从JSON对象中获取JArrays。看起来像这样:

public static JArray ToJArray(this JToken token, string itemProperty)
{
    if (token != null && token.Type != JTokenType.Null)
    {
        token = token[itemProperty];
        if (token != null)
        {
            if (token.Type == JTokenType.Array)
            {
                return (JArray)token;
            }
            else
            {
                return new JArray(token);
            }
        }
    }
    return new JArray();
}

上面的源代码来自这篇StackOverflow文章:Link

接下来,您必须记住,您仍然可以获得数据中的根对象。你必须解决它。在上面的链接中,您会看到他的来源看起来像这样。它演示了如何使用它。

class Program
    {
        static void Main(string[] args)
        {
            string json = @"
            {
              ""collection1"": {
                ""item"": {
                  ""label"": ""A"",
                  ""value"": ""1""
                }
              },
              ""collection2"": {
                ""item"": [
                  {
                    ""label"": ""B"",
                    ""value"": ""2""
                  },
                  {
                    ""label"": ""C"",
                    ""value"": ""3""
                  }
                ]
              },
              ""collection3"": null
            }";

            JObject root = JObject.Parse(json);

            DumpItems(root, "collection1");
            DumpItems(root, "collection2");
            DumpItems(root, "collection3");
        }

        private static void DumpItems(JToken token, string collectionName)
        {
            JArray array = token[collectionName].ToJArray("item");

            Console.WriteLine("Count of items in " + collectionName + ": " + array.Count);
            foreach (JToken item in array)
            {
                Console.WriteLine(item["label"] + ": " + item["value"]);
            }
        }
    }

以下是我如何解决这些问题的结果'结果'根。在我的示例中,列表中的每个项目都包含ID,名称和标签字符串。

public List GetResultsWithTag(string tagSrc)     {         // json数据中的Results对象是一个JObject。它包含一个结果对象的JSON数组

    JObject root = JObject.Parse(jsonData);

    JArray array = DumpItems(root, "results");

    List<Result> returnList = new List<Result>();

    foreach(var item in array)
    {
        Debug.WriteLine(item.ToString());
        Result _result = new Result();
        _result.ID = item["ID"].ToString();
        _result.Name = item["Name"].ToString();
        _result.Tags = item["Tags"].ToString();

        if(_result.Tags.Contains(tagSrc))
        {
            returnList.Add(_result);
        }

    }


    return returnList;
}
相关问题