【问题标题】:Cannot implicitly convert type 'Newtonsoft.Json.Linq.JArray[]' to 'Newtonsoft.Json.Linq.JToken'无法将类型“Newtonsoft.Json.Linq.JArray[]”隐式转换为“Newtonsoft.Json.Linq.JToken”
【发布时间】:2017-08-02 18:15:52
【问题描述】:
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”

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

【问题讨论】:

  • feature 是什么类型? item 是什么类型? Responseobj是什么类型的?
  • JObject 特征 = new JObject();
  • 餐厅项目 = new Resturant();
  • 类餐厅{公共字符串名称{获取;放; } 公共双纬度 { 获取;放; } 公共双经度 { 得到;放; } }

标签: c# json.net


【解决方案1】:

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

您需要一个帮助类来从 JSON 对象中获取 JArray。看起来像这样:

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、一个名称和一个标签字符串。

公共列表 GetResultsWithTag(string tagSrc) { // json数据中的Results对象是一个JObject。它包含一个 Result 对象的 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;
}

【讨论】:

    猜你喜欢
    • 2018-10-02
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    相关资源
    最近更新 更多