0

I am working on a unity project where i have to parse list of image url from JSON file and add it as source image to the dynamically spwaned scrollview content. I have created serializable class and dynamic spwaner , which will spwan the button on scrollview dynamically . How can i populate this list with the present JSON images URL i have . Thank you

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;

[System.Serializable]
public class Person
{
 public string name;
 public Sprite icon;
}

public class CreateScrollList : MonoBehaviour
{

 public GameObject namePrefab;
 public List<Person> personList;

 public Transform contentPanel;

 // Use this for initialization
 void Start()
 {

  PopulateList();
 }

 void PopulateList()
 {
  foreach (var person in personList)
  {
   GameObject newPerson = Instantiate(namePrefab) as GameObject;
   NamePrefab generatedName = newPerson.GetComponent<NamePrefab>();
   generatedName.nameLabel.text = person.name;
   generatedName.icon = person.icon;
   newPerson.transform.SetParent(contentPanel);

  }
 }
}

{
  "array": [
    "https://cdn1.iconfinder.com/data/icons/hawcons/32/699297-icon-68-document-file-app-512.png",
    "https://d1gzq6u422bfcj.cloudfront.net/workflow_icons/99b9a7050c2f46b4b0b657e4ab0bedf4.png",
    "https://cfcdnpull-creativefreedoml.netdna-ssl.com/wp-content/uploads/2016/06/New-instagram-icon.jpg"
  ]
}
Divakar R
  • 773
  • 1
  • 8
  • 36
  • You can use http://json2csharp.com/ to generate a class for your `JSON` object. Then you can use the `JsonUtility` to deserialize the object and then you can use the `WWW` class to download your List of images as Textures. – Hristo Jul 21 '18 at 14:32
  • Can you please share some reference where i can get a clear picture on how to deserialize the object using Jsonutility i gone through the unity docs https://docs.unity3d.com/ScriptReference/JsonUtility.html which talks about FromJson and FromJsonOverwrite but i am not sure where should use the static methods mentioned. Thank you – Divakar R Jul 21 '18 at 15:22
  • 1
    Sure thing. https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity Actually in your case @Programmer made a really clear explanation of how to serialize and deserialize `JSON` objects. So you would only need to generate a simple class structure for your object. – Hristo Jul 21 '18 at 15:29

0 Answers0