Google Sheets JSON 数据

huangapple go评论105阅读模式
英文:

Google sheets JSON data

问题

I understand your request. Here is the translated code portion:

  1. 我是C#的新手。我有以下JSON数据。此外,我是通过从Google表格转换而来的。我正在尝试为我的Unity游戏制作排行榜。但是我无法将这些“values”分配给变量。我想获取“Name”,“Score”,“Event”,并将它们分配给一个变量。
  2. public struct Data{
  3. public string range;
  4. public string majorDimension;
  5. public List<List<string>> values;
  6. }
  7. IEnumerator getData()
  8. {
  9. UnityWebRequest www = UnityWebRequest.Get(googleSheetURL);
  10. yield return www.SendWebRequest();
  11. if (www.isNetworkError || www.isHttpError)
  12. {
  13. Debug.Log(www.error);
  14. }
  15. else
  16. {
  17. Data data = JsonUtility.FromJson<Data>(www.downloadHandler.text);
  18. Debug.Log(data.values);
  19. Debug.Log(data.majorDimension);
  20. Debug.Log(data.range);
  21. }
  22. www.Dispose();
  23. }

希望这对您有所帮助。祝您有美好的一天!

英文:

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.

  1. {
  2. &quot;range&quot;: &quot;Sheet1!A1:M112&quot;,
  3. &quot;majorDimension&quot;: &quot;ROWS&quot;,
  4. &quot;values&quot;: [
  5. [
  6. &quot;Time&quot;,
  7. &quot;Name&quot;,
  8. &quot;Surname&quot;,
  9. &quot;E-Mail&quot;,
  10. &quot;Company / University&quot;,
  11. &quot;Event(Optional)&quot;,
  12. &quot;Score&quot;
  13. ],
  14. [
  15. &quot;12.06.2023 11:17:09&quot;,
  16. &quot;Henry&quot;,
  17. &quot;Powell&quot;,
  18. &quot;@hotmail.com&quot;,
  19. &quot;x Company&quot;,
  20. &quot;x Event&quot;,
  21. &quot;0&quot;
  22. ]
  23. ]
  24. }

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.

  1. public struct Data{
  2. public string range;
  3. public string majorDimension;
  4. public List&lt;List&lt;string&gt;&gt; values;
  5. }
  1. IEnumerator getData()
  2. {
  3. UnityWebRequest www = UnityWebRequest.Get(googleSheetURL);
  4. yield return www.SendWebRequest();
  5. if (www.isNetworkError || www.isHttpError)
  6. {
  7. Debug.Log(www.error);
  8. }
  9. else
  10. {
  11. Data data = JsonUtility.FromJson&lt;Data&gt;(www.downloadHandler.text);
  12. Debug.Log(data.values);
  13. Debug.Log(data.majorDimension);
  14. Debug.Log(data.range);
  15. }
  16. www.Dispose();
  17. }

I will be glad if you help. Have a nice day!

答案1

得分: 1

你可以使用这段示例代码:

  1. List<List<string>> values = data.values;
  2. int NameIndex = values[0].IndexOf("Name");
  3. int EventIndex = values[0].Select((ev, index) => (ev, index))
  4. .First(ev => ev.ev.Contains("Event")).index;
  5. int ScoreIndex = values[0].IndexOf("Score");
  6. var scores = values.Select((ev, index) => (ev, index)).Where(v => v.index > 0)
  7. .Select(e => new
  8. {
  9. name = e.ev[NameIndex],
  10. ev = e.ev[EventIndex],
  11. score = e.ev[ScoreIndex]
  12. }).ToList();

但我强烈建议你下载并安装 Unity3d 中的 Newtonsoft.Json(请自行搜索)。这样,你可以使用以下代码来反序列化 JSON 字符串:

  1. using Newtonsoft.Json;
  2. Data data = JsonConvert.DeserializeObject<Data>(www.downloadHandler.text);

但别忘了在你的 Data 类的所有属性上添加 {get; set;} 以便使用 Newtonsoft。

英文:

you can use this code for example

  1. List&lt;List&lt;string&gt;&gt; values = data.values;
  2. int NameIndex = values[0].IndexOf(&quot;Name&quot;);
  3. int EventIndex = values[0].Select((ev, index) =&gt; (ev, index))
  4. .First(ev =&gt; ev.ev.Contains(&quot;Event&quot;)).index;
  5. int ScoreIndex = values[0].IndexOf(&quot;Score&quot;);
  6. var scores = values.Select((ev, index) =&gt; (ev, index)).Where(v =&gt; v.index &gt; 0)
  7. .Select(e =&gt; new
  8. {
  9. name = e.ev[NameIndex],
  10. ev = e.ev[EventIndex],
  11. score = e.ev[ScoreIndex]
  12. }).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

  1. using Newtonsoft.Json;
  2. Data data = JsonConvert.DeserializeObject&lt;Data&gt;(www.downloadHandler.text);

But to use Newtonsoft don't forget to add {get; set;} to all properties of your Data class

huangapple
  • 本文由 发表于 2023年6月12日 21:51:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定