0

my code in unity c# is:

IEnumerator Start()
{
    UnityWebRequest wr = UnityWebRequest.Get("http://localhost:55150/api/values");
    yield return wr.SendWebRequest();
    string a = wr.downloadHandler.text;
    Debug.Log(a);
}

Received response from api, As follows:

[{"id":1,"name":"Leon","family":"ggg","des":"hhhhastam."},{"id":2,"name":"Ali","family":"dsf","des":"ali joon hastam."}]

how can i display names in foreach (i have fallow using litjson in project)

Leon
  • 11
  • 4
  • Possible duplicate of [Deserialize JSON with C#](https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) – Diado Feb 18 '19 at 13:11
  • I don't have the code right now but you can start by creating a class to serialize your json to a C# Object. You will than be able to get each property as a value – LiefLayer Feb 18 '19 at 13:11
  • take a look to my response here (after "Final EDIT" part) This is the default way Unity works with json (you don't need any library): https://stackoverflow.com/questions/51193503/converting-arrays-of-arrays-to-json/51193771#51193771 – LiefLayer Feb 18 '19 at 13:14

2 Answers2

3

If you don't want to write a class for mirroring the entire JSON structure you could use SimpleJSON (copy paste the SimpleJSON.cs into your Assets) and do something like

IEnumerator Start()
{
    UnityWebRequest wr = UnityWebRequest.Get("http://localhost:55150/api/values");
    yield return wr.SendWebRequest();
    string a = wr.downloadHandler.text;
    Debug.Log(a);

    var jsonObject = JSON.Parse(a);
    foreach (var element in jsonObject)
    {
        var elementName = element.Value["name"];
        // do something with elementName 
        Debug.Log(elementName);
    }
}

Update: sorting

Since you requested also sorting: You can do that using Linq OrderBy and OrderByDescending. Luckily SimpleJSON already has an implementation for that so you can use .Linq to make jsonObject an IEnumerable:

using System.Linq;

//...

var jsonObject = JSON.Parse(a);
foreach (var element in jsonObject.Linq.OrderByDescending(kvp => int.Parse(kvp.Value["id"])))
{
    var elementName = element.Value["name"];
    // do something with elementName 
    Debug.Log(elementName);
}

Note that this might throw exceptions for any wrong formatted JSON string.


Otherwise you have to implement the whole structure as class (and a wrapper since you get a List) and can use Unity JsonUtility

[Serializable]
public class DataList
{
    public List<DataItem> items = new List<DataItem>();
}

[Serializable]
public class DataItem
{
    public int id;
    public string name;
    public string family;
    public string des;
}

and do

IEnumerator Start()
{
    UnityWebRequest wr = UnityWebRequest.Get("http://localhost:55150/api/values");
    yield return wr.SendWebRequest();
    string a = wr.downloadHandler.text;
    Debug.Log(a);

    DataList dataList = JsonUtility.FromJson<DataList>(a);

    foreach (DataItem item in dataList.items)
    {
        // do something with element.name 
        Debug.Log(item.name);
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
2
  1. Define a class that represents each list item:

    class Item
    {
        public Item( Int32 id, String name, String family, String des )
        {
            this.Id     = id;
            this.Name   = name;
            this.Family = family;
            this.Des    = des;
        } 
    
        public Int32  Id     { get; }
        public String Name   { get; }
        public String Family { get; }
        public String Des    { get; }
    }
    
  2. Deserialize using Json.NET (Newtonsoft.Json)

    using Newtonsoft.Json;
    
    // ...
    
    String jsonText = wr.downloadHandler.text;
    List<Item> itemList = JsonConvert.DeserializeObject<List<Item>>( jsonText );
    
    foreach( Item item in itemList )
    {
        Debug.Log( item.Name );
    }
    
Dai
  • 141,631
  • 28
  • 261
  • 374