我已经从API将图像列表及其详细信息加载到了自动扩展的gridview中。以下是有关如何将API的详细信息加载到gridview中的代码。我左侧有一个汉堡菜单,它具有类别视图。例如,如果我有许多动态类别,如何从已加载的图像和详细信息显示在Gridview中。
List<int> assetiid = new List<int>();
List<string> productname = new List<string>();
List<string> productcatogery = new List<string>();
List<string> productcatogeryid = new List<string>();
List<string> productdesc = new List<string>();
List<int> assetpriceurl = new List<int>();
List<string> assetimageurl = new List<string>();
List<string> assetimageurl1 = new List<string>();
List<string> assetimageurl2 = new List<string>();
List<string> assetimageurl3 = new List<string>();
List<string> assetmodelurl = new List<string>();
string JSONDataString;
public static PopulateGrid Instancepop;
public void Awake()
{
Instancepop = this;
}
// Use this for initialization
public void Start ()
{
StartCoroutine(GetData());
}
// Update is called once per frame
void Update ()
{
}
IEnumerator GetData()
{
string URL = "http://test.com/test/api/products";//fake api
WWW readjson = new WWW(URL);
yield return readjson;
if (string.IsNullOrEmpty(readjson.error))
{
JSONDataString = readjson.text;
}
JSONNode JNode = SimpleJSON.JSON.Parse(JSONDataString);
for (int i = 0; i < JNode.Count; i++)
{
//Debug.Log("Image URLS before = " + JNode[i]["product_images"][0]);
assetiid.Add(JNode[i]["id"]);
productname.Add(JNode[i]["product_name"]);
productcatogery.Add(JNode[i]["product_category"]);
productcatogeryid.Add(JNode[i]["product_category_id"]);
productdesc.Add(JNode[i]["product_description"]);
assetpriceurl.Add(JNode[i]["product_price"]);
assetimageurl.Add(JNode[i]["product_images"][0]);
assetimageurl1.Add(JNode[i]["product_images"][1]);
assetimageurl2.Add(JNode[i]["product_images"][2]);
assetimageurl3.Add(JNode[i]["product_images"][3]);
assetmodelurl.Add(JNode[i]["product_model"]);
Debug.Log("Image URLS = "+assetimageurl[i] );
}
StartCoroutine(FillData());
}
IEnumerator FillData()
{
//for (int i = 0; i < assetimageurl.Count; i++)
for (int i = 0; i < assetimageurl.Count; i++)
{
Texture2D tex;
tex = new Texture2D(150, 150, TextureFormat.DXT1, false);
WWW imgurllinks = new WWW(assetimageurl[i]);
yield return imgurllinks;
if (string.IsNullOrEmpty(imgurllinks.error))
{
yield return null;
}
imgurllinks.LoadImageIntoTexture(tex);
rimg.texture = tex;
//RawImage newItem = Instantiate(rimg) as RawImage;
//newItem.transform.SetParent(ParentTransform.transform,false);
Productxt.text = productname[i];
Pricetxt.text = assetpriceurl[i].ToString();
Instantiate(Mainprefab, ParentTransform);
}
}
下面是所有产品详细信息API(未显示完整API)
"id":1,"product_name":"Dining Chair ","product_category_id":3
下面是类别API
[{"id":1,"category":"Chairs"},{"id":2,"category":"Tables"},{"id":3,"category":"Beds"}]
类别API中的“ id”与完整产品API中的“ product_category_id”相匹配,以根据类别进行显示。
如果我在类别视图中单击椅子,我想显示已加载的详细信息中包含“ product_category_id” = 3的产品的所有详细信息
//以下功能在不同的类上
public void ButtonClickedOn(int buttonNo)
{
buttonNo = Index;
Debug.Log("Button clicked = " + buttonNo);
if(buttonNo==0)
{
//Category 1 is clicked
}
}
答案 0 :(得分:0)
使用Json.NET将json
反序列化为C# Object
。然后使用linq
过滤具有特殊类别的列表。
这是一个简单的console
工作示例
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
class ProductDetails
{
public int id { get; set; }
public string product_name { get; set; }
public int product_category_id { get; set; }
}
class Category
{
public int id { get; set; }
public string category { get; set; }
}
class Program
{
static void Main()
{
string productJson = @"[{'id': '1', 'product_name':'test', 'product_category_id': '1'},
{'id': '2', 'product_name':'Dining Chair', 'product_category_id': '3'},
{'id': '3', 'product_name':'Dining Chippsss', 'product_category_id': '3'}]";
string categoryJson = @"[{ 'id':'1','category':'Chairs'},
{ 'id':'2','category':'Tables'},
{ 'id':'3','category':'Beds'}]";
var categories = JsonConvert.DeserializeObject<List<Category>>(categoryJson);
var products = JsonConvert.DeserializeObject<List<ProductDetails>>(productJson);
// Finding tables category
var query = (from x in products
where x.product_category_id == 3
select x);
foreach (var product in query)
{
var category = categories.SingleOrDefault(x => x.id == product.product_category_id);
Console.WriteLine("Product Name: " + product.product_name +
" - Product Category: " + category.category);
}
}
}