【问题标题】:Nested class and save json problem in UnityUnity中的嵌套类和保存json问题
【发布时间】:2019-10-18 02:28:31
【问题描述】:

我有这段代码来保存 json,但不是每次点击按钮时都添加新礼物,它只是用新数据替换 GiftDetails 的第二部分。

我需要编辑或添加什么代码来防止这种情况发生,这样它才能完美地添加新的 GiftDetail?

我的json:

{
    "GValue": 1,
    "GName": "Father Gift",
    "GiftDetails": [
      {
        "GDetailValue": 5,
        "GDetailName": "3",
        "GDetailWeight": 2.0,
        "GDetailAmount": 4
      },
      {
        "GDetailValue": 2,
        "GDetailName": "Gift1",
        "GDetailWeight": 11.0,
        "GDetailAmount": 213
      }
    ]
  },

我的班级:

[System.Serializable]
public class GiftDetail
{
    public int GDetailValue;
    public string GDetailName;
    public double GDetailWeight;
    public int GDetailAmount;
}
[System.Serializable]
public class Gift
{

    public int GValue;
    public string GName;
    public List<GiftDetail> GiftDetails;
}

我的按钮:

            GiftDetail NewGiftDetail = new GiftDetail
            {
                GDetailValue = 5,
                GDetailAmount = 4,
                GDetailName = "Gift1",
                GDetailWeight = 2,
            };

            Gift NewGift = new Gift
            {
                GValue = DropDownValueQua,
                GName = txtDropDownQuaTxt,
                GiftDetails = new List<GiftDetail>() { NewGiftDetail }
            };

            if (_Gift.Exists(x => x.GValue == 1 ))//&&_Gift.Exists(x => x.GDetailValue == DropDownValuePhanQua))
             //dont' know how to call GDetailValue here
            {
              GiftDetail NewGiftDetail3 = new GiftDetail
              {
                 GDetailValue = DropDownValuePhanQua,
                 GDetailAmount = iSoLuongPhanQua,
                 GDetailName = ifTenPhanQua.text,
                 GDetailWeight = fTileRaPhanQua,
              };

              int i = 0;

              while (i <_Gift.Count && _Gift[i].GValue != 1)// &&_Gift[i].GDetailValue !=DropDownValuePhanQua)
               //and here
                    i++;


              _Gift.RemoveAt(i);
              Debug.Log(  "Edit Success");
              NewGift.GiftDetails.Add(NewGiftDetail3);

            }
            _Gift.Add(NewGift);
            SaveToJson(_Gift, DATABASE_NAME);

以及如何访问 GDetailValue = 2 以便我可以从数据库中删除、编辑该礼物而不影响其他礼物。 有没有这样的方法?

if (_Gift.Exists(x => x.GValue == DropDownValueQua ))

但不是

X.GValue 是 X.GiftDetail.GDetailAmount ???

如果我错过了什么让你们不明白,请告诉我^_^



这就是我刚刚发现的,如果我以正确的顺序创建父亲礼物,它会很好,如果错误的顺序比它会说索引超出范围。无论如何要获得正确的订单,以便我可以添加儿童礼物?

GiftDetail NewGiftDetail3 = new GiftDetail
        {
            GDetailValue = DropDownValuePhanQua,
            GDetailAmount = iSoLuongPhanQua,
            GDetailName = ifTenPhanQua.text,
            GDetailWeight = fTileRaPhanQua,
        };
        bool isGiftFound = false;
        int s = 0;
        while (s < _Gift.Count) //&& isGiftFound == false)
        {
            for (int z = 0; z < _Gift[s].GiftDetails.Count; z++)
            {
                if (_Gift[s].GiftDetails[z].GDetailValue == DropDownValuePhanQua && _Gift[s].GValue==DropDownValueQua)
                {
                    isGiftFound = true;
                    string qweqw = _Gift[s].GiftDetails[z].GDetailName.ToString();
                    Debug.Log(qweqw);
                    _Gift[s].GiftDetails.RemoveAt(z);
                    _Gift[s].GiftDetails.Add(NewGiftDetail3);
                }
                else {

                }

            }
            s++;

        }


        if (isGiftFound == false)
        {
            _Gift[DropDownValueQua-1].GiftDetails.Add(NewGiftDetail3);
            //this code fail if I don't create Father Gift in right order of DropDownValueQua. Is there anyway else.
        }

【问题讨论】:

  • 我不认为它可以是其他任何东西,因为列表被称为GiftDetail,即使作为数组,它也会是......
  • @BugFinder 我找到了解决方法,但现在我无法获得正确的父亲礼物来添加孩子礼物。我只是添加我的代码
  • 好的,但是一个新问题应该是一个新的帖子,没有贴在这个帖子的末尾。也没有被新问题取代
  • 但我确实首先询问了如何将孩子的礼物添加到父亲的礼物,但一开始是完美的。子问题是如何调用 GDetailValue=2。

标签: c# json unity3d


【解决方案1】:

以及如何访问 GDetailValue = 2

如果您只需将 GDetailValue 与您从用户输入中收到的 dropdownValue 的值进行比较,请尝试以下行

var resultGift = gift.GiftDetails.FirstOrDefault(gd => gd.GDetailValue == DropDownValuePhanQua);

输出:

【讨论】:

  • 我使用这个并且 resultGift 返回 null 即使 GDetailValue 和 DropDownValuePhanQua 具有相同的值,如果我不这样做,它不会让我访问 GiftDetails _Gift[0].GiftDetails
  • var resultGift = _Gift[0].GiftDetails.FirstOrDefault(gd => gd.GDetailValue == DropDownValuePhanQua);
  • 如果只比较我认为这更好 if(_Gift[i].GiftDetails.Exists(x => x.GDetailValue == DropDownValuePhanQua )) 但我可以获得 GiftDetail[] 删除的地方,编辑它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-20
  • 1970-01-01
  • 2021-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多