繁体   English   中英

无法将类型“Newtonsoft.Json.Linq.JArray[]”隐式转换为“Newtonsoft.Json.Linq.JToken”

[英]Cannot implicitly convert type 'Newtonsoft.Json.Linq.JArray[]' to 'Newtonsoft.Json.Linq.JToken'

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[]”隐式转换为“Newtonsoft.Json.Linq.JToken”

请帮我正确地做这件事

事实证明我正在解决同样的问题。 在对 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 文章: 链接

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

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 Result 对象数组

    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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM