英文:
Google sheets JSON data
问题
I understand your request. Here is the translated code portion:
我是C#的新手。我有以下JSON数据。此外,我是通过从Google表格转换而来的。我正在尝试为我的Unity游戏制作排行榜。但是我无法将这些“values”分配给变量。我想获取“Name”,“Score”,“Event”,并将它们分配给一个变量。
public struct Data{
public string range;
public string majorDimension;
public List<List<string>> values;
}
IEnumerator getData()
{
UnityWebRequest www = UnityWebRequest.Get(googleSheetURL);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Data data = JsonUtility.FromJson<Data>(www.downloadHandler.text);
Debug.Log(data.values);
Debug.Log(data.majorDimension);
Debug.Log(data.range);
}
www.Dispose();
}
希望这对您有所帮助。祝您有美好的一天!
英文:
I am new in c#. I have JSON data like below. Furthermore, I got it by converting from Google sheets. I am trying to make a leaderboard for my unity game. But I can't assign these "values" to the variable. I want to get "Name", "Score", "Event" and I want to assign them to a variable.
{
"range": "Sheet1!A1:M112",
"majorDimension": "ROWS",
"values": [
[
"Time",
"Name",
"Surname",
"E-Mail",
"Company / University",
"Event(Optional)",
"Score"
],
[
"12.06.2023 11:17:09",
"Henry",
"Powell",
"@hotmail.com",
"x Company",
"x Event",
"0"
]
]
}
I have structure like this. Furthermore, I can print majorDimension
and range. I don't understand list inside list concept in c# so i can't reach "values" section. You can see what i tried in getData section.
public struct Data{
public string range;
public string majorDimension;
public List<List<string>> values;
}
IEnumerator getData()
{
UnityWebRequest www = UnityWebRequest.Get(googleSheetURL);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Data data = JsonUtility.FromJson<Data>(www.downloadHandler.text);
Debug.Log(data.values);
Debug.Log(data.majorDimension);
Debug.Log(data.range);
}
www.Dispose();
}
I will be glad if you help. Have a nice day!
答案1
得分: 1
你可以使用这段示例代码:
List<List<string>> values = data.values;
int NameIndex = values[0].IndexOf("Name");
int EventIndex = values[0].Select((ev, index) => (ev, index))
.First(ev => ev.ev.Contains("Event")).index;
int ScoreIndex = values[0].IndexOf("Score");
var scores = values.Select((ev, index) => (ev, index)).Where(v => v.index > 0)
.Select(e => new
{
name = e.ev[NameIndex],
ev = e.ev[EventIndex],
score = e.ev[ScoreIndex]
}).ToList();
但我强烈建议你下载并安装 Unity3d 中的 Newtonsoft.Json(请自行搜索)。这样,你可以使用以下代码来反序列化 JSON 字符串:
using Newtonsoft.Json;
Data data = JsonConvert.DeserializeObject<Data>(www.downloadHandler.text);
但别忘了在你的 Data 类的所有属性上添加 {get; set;}
以便使用 Newtonsoft。
英文:
you can use this code for example
List<List<string>> values = data.values;
int NameIndex = values[0].IndexOf("Name");
int EventIndex = values[0].Select((ev, index) => (ev, index))
.First(ev => ev.ev.Contains("Event")).index;
int ScoreIndex = values[0].IndexOf("Score");
var scores = values.Select((ev, index) => (ev, index)).Where(v => v.index > 0)
.Select(e => new
{
name = e.ev[NameIndex],
ev = e.ev[EventIndex],
score = e.ev[ScoreIndex]
}).ToList();
but I highly recommend you do download and install Newtonsoft.Json for Unity3d (google it). In this case you can deserialize a json string using this code
using Newtonsoft.Json;
Data data = JsonConvert.DeserializeObject<Data>(www.downloadHandler.text);
But to use Newtonsoft don't forget to add {get; set;} to all properties of your Data class
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论