【问题标题】:How to get image URLs from inside an array of array using Simple JSON in unity?如何统一使用简单 JSON 从数组数组中获取图像 URL?
【发布时间】:2019-03-25 11:18:29
【问题描述】:

当我尝试将 url 的 JSON 数组转换为字符串列表时发生错误。错误显示为:

Argument `#7' cannot convert `SimpleJSON.JSONNode' expression to type `System.Collections.Generic.List<string>'

下面给出了我的 Json 数据处理程序类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



[System.Serializable]
public class Handlepojo
{
public int astid;
public string prdname;
public int catid;
public string catname;
public string catdesc;
public int prdprice;

public List<string> Urls;


public Handlepojo(int assetid, string productname, int categid, string categoname, string categodesc, int productprice, List<string> Allurls)
{
    this.astid= assetid;
    this.prdname = productname;
    this.catid = categid;
    this.catname = categoname;
    this.catdesc = categodesc;
    this.prdprice = productprice;
    this.Urls = Allurls;


}


}

以下是我如何将 JSON 数据添加到单个列表中。

for (int i = 0; i < JNode.Count;i++)
    {

        Alldetails.Add(new Handlepojo(JNode[i]["id"], JNode[i]["product_name"], JNode[i]["product_category_id"], JNode[i]["product_category_name"],
        JNode[i]["product_description"], JNode[i]["product_price"],JNode[i]["product_images"]));
    }

我的 JSON 值如下所示

{
    "id": 1,
    "product_name": "Wood Chair",
    "product_category_id": 3,
    "product_category_name": "Chair",
    "product_description": "Tough Hard wood",
    "product_price": "100",
    "product_images":
    [
        "http://test.com/testing/storage//productphotos/HYTh3zUYjQKuHavNSpjQ1xeUq7laeS1WwOKPOkpQ.jpeg",
        "http://test.com/testing/storage//productphotos/h01SOXObWmF07OCKesMFOacK4LRCpU8Rl14T6b1Z.jpeg",
        "http://test.com/testing///productphotos/cWQG7Xpkdhht1218xg5gPYaYDoi6pJPzt7MDhBqY.jpeg",
        "http://test.com/testing///productphotos/P64UvFr7vQidwSkKvGQjwebSCOAoHCXLfxijtPND.jpeg",
        "http://test.com/testing///productphotos/tKt8rf0FYHqYFlqMD3tqgTydqRYMFeKZBZiP7oMN.jpeg"
    ]
}

但在最后一行 Jnode["product_images"] 错误显示

" cannot convert `SimpleJSON.JSONNode' expression to type `System.Collections.Generic.List<string>' . " 

如何将它们添加到单个列表中?我的做法是使用另一个列表,然后根据assetid和相应的URL添加。为什么我不能使用SimpleJson将字符串中的JSONarray添加到列表中?

【问题讨论】:

  • 您通过 JNode[0]["product_images"][0] 获得图像 url。循环你的 JNode[0]["product_images"]

标签: c# json unity3d


【解决方案1】:

product_images 字段比其他类型更复杂,因此您可以先尝试处理它。我没有使用您正在使用的 JNode 类型/库的经验,但我希望您能够按照以下方式做一些事情:

  for (int i = 0; i < JNode.Count;i++)
    {

    List<string> Imagestring = new List<string>();
        // See what type we get from the relevant field

     for (int j = 0; j < JNode[i]["product_images"].Count;j++)
        {
            var imgeurls = JNode[i]["product_images"][j];
            Imagestring.Add(imgeurls.ToString());

        }

        // Note - the last item is now the array created above
        Alldetails.Add(new Handlepojo(JNode[i]["id"], JNode[i]["product_name"], JNode[i]["product_category_id"], JNode[i]["product_category_name"],
        JNode[i]["product_description"], JNode[i]["product_price"],Imagestring));
    }

【讨论】:

  • ....imageUrlNode 只返回一个字段...我该如何运行 foreach 循环?
  • 从您的代码中获得概念...根据我的需要进行编辑...:)
【解决方案2】:

这是使用 SimpleJSON 将链接添加到字符串列表的方法。

    JSONNode jnode = JSON.Parse(data);
    JSONArray jLinks = jnode["product_images"].AsArray;
    for (int i = 0; i < jLinks.Count; i++)
    {
        links.Add(jLinks[i]);
    }

这里的“数据”是你的 json 文件的文本格式。

【讨论】:

    猜你喜欢
    • 2014-10-27
    • 2012-12-07
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 2021-08-27
    • 1970-01-01
    相关资源
    最近更新 更多