JSON创建方法

时间:2018-01-31 12:23:32

标签: ios arrays json swift

我正在开发一个IOS项目。我正在尝试创建Twitter。我正在使用Firebase数据库。如您所知,Firebase使用JSON,我对数据库创建感到困惑。

首先,我的用户有姓名,姓名,电子邮件,用户名和个人资料网址。

此外,我的帖子中有一个名为post的字符串,但您知道我需要在帖子中显示用户。 post对象有其用户名,profilepic和用户名

用户也可以关注其他用户,他们的时间表应该只有他们关注的用户发送的帖子。

这个结构让我很困惑这里是我找到的解决方案和我的json文件的例子

{
"users" : {
"1SUbzM6rIRTQexrOgJ8BnDBCDWt2" : {
  "email" : "Test1@test.com",
  "fullname" : "Test1 Test1",
  "name" : "Test1",
  "surname" : "Test1",
  "username" : "@test1",
  "ppurl" : "www.ppurl.com/ppppp",
  "posts" : {}
},
"4vBvO9vURkPneusviRGxKglJ3n32" : {
    "email" : "Test2@test.com",
    "fullname" : "Test2 Test2",
    "name" : "Test2",
    "surname" : "Test2",
    "username" : "@test2",
    "ppurl" : "www.ppurl.com/ppppp",
    "posts" : {
            "34E20A52-8E66-4AF7-8DA4-73BDE9185FCB" : {
                "post" : "Bir post daha\n"
            },

            "59798B81-4510-4E63-8050-3AF04698C7B0" : {
                "post" : "3. Postumuz gör better approach"
                }
            },
    "follows" : {
        "5QaOU5Pd05h2M8wExcUteUg6mlJ2" : {
            "email" : "TEST3@TEST.com",
            "fullname" : "TEST3 TEST3",
            "name" : "TEST3",
            "surname" : "TEST3"
            "username" : "@test3",
            "ppurl" : "www.ppurl.com/ppppp",
            "posts" : {}
            },
        "6y0RLGGCw6Zg5RHgPxghKUId9pJ3" : {
            "email" : "TEST4@jjj.hhh",
            "fullname" : "TEST4 TEST4",
            "name" : "TEST4",
            "surname" : "TEST4",
            "username" : "@test4",
            "ppurl" : "www.ppurl.com/ppppp",
            "posts" : {}
            },

    },
},      
"5QaOU5Pd05h2M8wExcUteUg6mlJ2" : {
  "email" : "TEST3@TEST.com",
  "fullname" : "TEST3 TEST3",
  "name" : "TEST3",
  "surname" : "TEST3"
  "username" : "@test3",
  "ppurl" : "www.ppurl.com/ppppp",
  "posts" : {}
},
"6y0RLGGCw6Zg5RHgPxghKUId9pJ3" : {
  "email" : "TEST4@jjj.hhh",
  "fullname" : "TEST4 TEST4",
  "name" : "TEST4",
  "surname" : "TEST4"
  "username" : "@test4",
  "ppurl" : "www.ppurl.com/ppppp",
  "posts" : {}
  }

}

这是一种正确的方法吗?用户和帖子重复,很难从swift获取数据。我应该保存与用户不同的帖子,但如果我使用这种方法,我就无法获得帖子的用户名,名称和ppurl等。

我应该如何构建我的JSON文件并使用最有效的方式创建关系。

编辑:

我可以将我的json文件存入我的项目。我的问题是:写同一个用户2-3次是真的吗?我有一个用户已经在我的json文件中,但当有人跟随某人时我应该在friends属性中再写一次吗?或者我可以仅使用id获得参考。

1 个答案:

答案 0 :(得分:0)

为什么不使用像这样简单的结构:

{
    "users": [
        {
            "identifier": "1SUbzM6rIRTQexrOgJ8BnDBCDWt2",
            "email": "Test1@test.com",
            "fullname": "Test1 Test1",
            "name": "Test1",
            "surname": "Test1",
            "username": "@test1",
            "ppurl": "www.ppurl.com/ppppp"
        },
        {
            "identifier": "4vBvO9vURkPneusviRGxKglJ3n32",
            "email": "Test2@test.com",
            "fullname": "Test2 Test2",
            "name": "Test2",
            "surname": "Test2",
            "username": "@test2",
            "ppurl": "www.ppurl.com/ppppp",
            "posts": [
                "34E20A52-8E66-4AF7-8DA4-73BDE9185FCB",
                "59798B81-4510-4E63-8050-3AF04698C7B0"
            ],
            "follows": [
                "5QaOU5Pd05h2M8wExcUteUg6mlJ2",
                "6y0RLGGCw6Zg5RHgPxghKUId9pJ3"
            ]
        },
        {
            "identifier": "5QaOU5Pd05h2M8wExcUteUg6mlJ2",
            "email": "TEST3@TEST.com",
            "fullname": "TEST3 TEST3",
            "name": "TEST3",
            "surname": "TEST3",
            "username": "@test3",
            "ppurl": "www.ppurl.com/ppppp"
        },
        {
            "identifier": "6y0RLGGCw6Zg5RHgPxghKUId9pJ3",
            "email": "TEST4@jjj.hhh",
            "fullname": "TEST4 TEST4",
            "name": "TEST4",
            "surname": "TEST4",
            "username": "@test4",
            "ppurl": "www.ppurl.com/ppppp"
        }
    ]
}

否则你的JSON有效载荷会随着时间的推移变得越来越大。

编辑刚刚看到@vadian评论......让我们说这是基于他的想法。