如何统一使用简单JSON从数组内部获取图像URL?

时间:2019-03-25 11:18:29

标签: c# json unity3d

当我尝试将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>' . " 

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

2 个答案:

答案 0 :(得分: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));
    }

答案 1 :(得分:0)

这是使用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文件的文本格式。