【发布时间】: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"]