【问题标题】:display json string name of json result in c#在c#中显示json结果的json字符串名称
【发布时间】:2019-02-19 05:04:52
【问题描述】:

我在统一 c# 中的代码是:

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

从api收到响应,如下:

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

如何在 foreach 中显示名称(我在项目中使用 litjson)

【问题讨论】:

  • Deserialize JSON with C#的可能重复
  • 我现在没有代码,但您可以先创建一个类来将您的 json 序列化为 C# 对象。您将能够将每个属性作为值获取
  • 看看我的回复(在“Final EDIT”部分之后)这是 Unity 使用 json 的默认方式(您不需要任何库):stackoverflow.com/questions/51193503/…

标签: c# unity3d


【解决方案1】:

如果您不想编写用于镜像整个 JSON 结构的类,您可以使用 SimpleJSON(将 SimpleJSON.cs 复制粘贴到您的资产中)并执行类似的操作

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);
    }
}

更新:排序

由于您还要求排序:您可以使用 Linq OrderByOrderByDescending 进行排序。幸运的是 SimpleJSON 已经有一个实现,所以你可以使用 .LinqjsonObject 变成 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);
}

注意,这可能会为任何格式错误的 JSON 字符串引发异常。


否则你必须将整个结构实现为类(和一个包装器,因为你得到一个列表)并且可以使用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;
}

然后做

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);
    }
}

【讨论】:

  • 非常感谢注意第一种方法(simplejson)如何按id值orderby decending?例如:阿里、莱昂
  • @Leon 请参阅Update sorting 部分
【解决方案2】:
  1. 定义一个代表每个列表项的类:

    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. 使用 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 );
    }
    

【讨论】:

    猜你喜欢
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多