Google Sheets JSON 数据

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

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.

{
  &quot;range&quot;: &quot;Sheet1!A1:M112&quot;,
  &quot;majorDimension&quot;: &quot;ROWS&quot;,
  &quot;values&quot;: [
    [
      &quot;Time&quot;,
      &quot;Name&quot;,
      &quot;Surname&quot;,
      &quot;E-Mail&quot;,
      &quot;Company / University&quot;,
      &quot;Event(Optional)&quot;,
      &quot;Score&quot;
    ],
    [
      &quot;12.06.2023 11:17:09&quot;,
      &quot;Henry&quot;,
      &quot;Powell&quot;,
      &quot;@hotmail.com&quot;,
      &quot;x Company&quot;,
      &quot;x Event&quot;,
      &quot;0&quot;
    ]
  ]
}

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&lt;List&lt;string&gt;&gt; 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&lt;Data&gt;(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&lt;List&lt;string&gt;&gt; values = data.values;

	int NameIndex = values[0].IndexOf(&quot;Name&quot;);
	int EventIndex = values[0].Select((ev, index) =&gt; (ev, index))
                              .First(ev =&gt; ev.ev.Contains(&quot;Event&quot;)).index;
	int ScoreIndex = values[0].IndexOf(&quot;Score&quot;);

var scores = values.Select((ev, index) =&gt; (ev, index)).Where(v =&gt; v.index &gt; 0)
							.Select(e =&gt;  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&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:

确定