|
Hi I'm struggling to understand JSON and wondering if anyone could either explain or point me somewhere to gain a better understanding? The way it is being introduced along with multi page apps, classes, protocols and different property observers, as a total beginner, I feel overwhelmed. I am just moving onto day 41 of the Moonshot app but have backtracked as I felt I was just getting lost in the fog. I get that it is used to store user settings and also to format files, but how it is doing it, and whether files need to be written in this way (or even how they are written in this way) I don't really get. As a way of explaining the depth of my confusion, I'm going use the example below that Paul uses (I think on day 37). I have added the print functions myself. He uses it to explain JSON but honestly I am not sure what I am supposed to take away from this. The data part prints '41 bytes' and the print(user) basically, and predictably, shows the variable 'User.' That's fine but has this data been saved somewhere? Has it been encoded? What even does it mean to encode the data? Also what is the 'forKey' "UserData" used for? Is the data stored in a dictionary? How do I retrieve it? I just can't really visualise what is going on even at this point so when he starts trying to incorporate it into apps with several pages, I just can't keep track of it. The basic understanding just isn't there.
|
|
you ask a very good question , you see we need to accpet data and also share data, so to come to any kind of agreement on how to do it universally is a bit difficult , so this json is a part of that agreement, so what happens is that we now say that we can transmit and accept data using this format, but what that actually means? So what happens is there might me a server some where and that server holds some info and say there is a browser , that wants to read that information ... so now how do we send a data that can be universally understood by browsers and then also in human readable format, that format is json, which is also stateless, means it does not remember its previous state ... this is what really json is you must see files like
now you can see, that there is a pattern, there is this "array", "boolean" etc and then some values, in front of them, true/false etc, this is one of the requirements in some languages like swift that it has to represent either an array or dictioary , ie, key/value pairing... but to boil it down, its an agreed format these days where we have to create a json file in a particular way and then the language will have feature to read it and use. the information ... your above example is just a way the swift language is trying to read and use that info from json file do not rush , these things took years to formualte , no one not even a maths phd will understand them fast .. good luck |
|
Many thanks for your response @AmitShrivastava. To clarify, what I am really looking for is an understanding of how JSON works in a practical sense within the program. What are the processes that happen when the button is pressed? What does it mean to 'encode' the data, and why does it need to be encoded/decoded? Is it saved somewhere, if so where and how do I access it? of course in more general terms, beyond the example I am also unclear exactly what a JSON file is, how it is created and whether you could say, ever use an ordinary text file in a program? I get that the format is made to be read in such a way that the program recognises a date as a date for instance (rather than a simple string) but I don't have any broader understanding of how you might use data within apps to know where JSON would and wouldn't be used. |
SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
this seems like precise article with some topics you want to understand - https://www.avanderlee.com/swift/json-parsing-decoding/ |
|
Thanks for the link. Unfortunately, it doesn't really help as I don't understand what he is trying to decode or why he is trying to decode it. I am several steps before this point. All I can see is that he has started off with some white text, written quite a complicated struct and ended up with the same text but in red. For the seasoned developer, I'm sure there is more to it but it means nothing to me at this time. My questions, I think are more fundamental, why is he using JSON data in the first place, how has he placed it into his program and what happens to it when someone accesses the site? Although, to be honest, that's even getting a bit complicated, which is why I used the example I did because really if someone could explain the process in terms of a button being pressed and a process being activated in very straightforward terms I would be even happier. |
|
@cj asks:
Like Amit, I agree this is a great question. And am pleased that you're not afraid to ask it in this forum. Many programmers here are greatly familiar with Exchanging DataIf I asked you to send me a list of what you ate for breakfast, you might reply: "Pop-tart, bacon (3 pieces), coffee" Amit might respond with:
My answer might be: "(a) Bagel, (b) cream cheese, (c) grapes, (d) Earl Grey (hot!)" The three of us instantly understand these formats. But please realize that none of us used the same format. Now, extend this data exchange problem to each student in your French course, your immediate family, your mates in Hufflepuff, and the 20+ people who rode your bus this morning. Your breakfast-data-exchange problem became torture! Data Exchange FormatSo first and foremost, think of
Now, if you send me your breakfast every day in this format, I'll know who, which day, which meal, and the items and drinks in the meal. Using this format, I can write a process to read As you can see, |
|
JSON: Part II Nice! You just sent me a JSON file listing your breakfast. It looks like this:
More QuestionsLet's answer your next question:
I received your meal file as a TEXT file in Indeed. In my application I have a
DecodingNice! Now I have the task of taking this nicely formed Also notice that Nightmare! The process of mapping the
EncodingThe opposite is called |
|
Many thanks for your response @Obelix. The idea of a data format that is universally readable and is then reformatted for different users makes sense. I had got confused by the terms encoding and decoding as if somehow something was being scrambled but it seems clear now that the encoding simply means - 'write' to JSON format and decode, 'read' from JSON format, albeit with some translation. So the button above is presumably writing the data to JSON format? I think I understand but, I am still a bit confused about where that data has been saved and how to access it. There is a 'key' given "UserData" but I am confused as to how I would use this? I know at some point we learnt about keys with dictionaries but it seems like some time ago and even looking back at it I can't quite put it togther in my mind. |
|
This line really has nothing to do with understanding what JSON is. It's just storing the data that was encoded to JSON in the previous line into the |
|
JSON Lesson: Part III Now that you have a little more clarity on See-> Codeable Cheat Sheet CodableSo why might you ask your application's Structs to conform to the The short answer is:
When you declare that your Struct conforms to the
Is it always possible to encode and decode your
If you design a Extra WorkIn these cases, you cannot simply turn your
How does Swift's encoder turn this This will be part of your design. If you want to encode the When you retreive the What is the best strategy?What is the best strategy for encoding or decoding Swift data types that don't conform to JSON's built in types? Hey! You're the programmer. This is your job to figure out. This is why you get paid the 'big bucks'. Good luck! Keep Coding! |
|
Many thanks @Obelix and @Roosterboy for your further explanations. It has helped clarify things a lot. @Roosterboy your comment has actually made me realise that I was, in additon to being confused about JSON, also confused by the use of User Defaults and I had linked them together in my head. So, if you don't mind, I just have a couple more questions based on this, using iExpense as an example. My understanding so far is that the struct below creates data which conforms to the codable protocol and so any data I add will be able to be converted to JSON. This is then 'mapped' (is that the right word?) to the array in the Expenses class(which is not quite like a normal array in the sense that it can be broken down to different data types as defined by the struct). If I understand correctly, when the user enters an expense, this should then be saved into UserDefaults, which means that it should be there when I return to the app after shutting down (if not it will presumably return an empty array (empty screen). Please feel free to correct me if I have got any of this wrong. Whilst I think I understand some of what's going on, I do have a couple of questions. Firstly, why are we encoding and decoding JSON data here? I understand the need for it when you have an external document in JSON format that you import into the app. You want to convert it to user friendly text but in this programme we don't have any external documentation in JSON format, so why are we converting to JSON, saving it to user defaults then converting it back? Or is that not what's happening here? I feel like I'm missing something. If we don't need to use JSON with user defaults then why are we? My second question is, if it is not already answered in response to the first, what exactly is User Defaults? Is it some kind of dictionary? And what is the key "Items" used for? I don't want to suddenly jump from JSON to User Defaults, it is just that I think I have merged them together in my head and realise now that my confusion was actually mulitifaceted -JSON, User Defaults and the interaction between the two.
|
|
It's great to see the lights come on! @cj concludes:
Yes! Data Exchange FormatRemember, first and foremost (from above), think of So, who are you exchanging data with? You aren't exchanging data with Obelix, nor with @roosterboy. You are exchanging data with future you. In your application, you have a great Struct for one But at some time, you'll have to put your iPhone down then go outside and touch some grass. So yeah, you need a strategy for saving all your expenses (in some format) and save them. At a future time, you'll want to grab those saved expenses and read them back into your application. Saving Application DataAs I recall, the structure of an iPhone application provides a private storage area where your application can save preferences (like what color you want for a background, or what cities you want in your Weather application). This private area can also be used to save data important to your user. You should consider differerent strategies depending on how much data you want to actually save. For example, if you're saving how many pushups you do each day, this could be a very small amount of data consisting of a date and an integer. You could save several years of data and not use very much space. On the other hand, if you're saving sound bites from course work, or riffs from jam sessions with your musician friends, your sound files may be huge and you'll be using a ton of storage. You may need to explore your options and pick an appropriate strategy. For this simple application, iExpense, you're just storing some imaginary expenses for a few imaginary purchases. For this lesson, @twoStraws selected the application's What are you storing in UserDefaultsAgain, as a teaching technique, @twoStraws is walking you through example code where you grab all the expenses in your application, convert them into a single The file will stay in @cj misses the point here:
In short, you are exchanging a Alternate Storage TechniquesTo be fair, you could save your expense data as a list of comma separated values (excel spreadsheet style). There are a number of strategies you could use to gather all your expenses, and save them to your iPhone. The downside is how do you read the comma separated values file? How do you match the third data element in the seventh line to your expense category? These are programming issues that are solved by using a convenient API built into the Swift language. |
|
Thank you @Obelix again for such an in depth explanantion. I understood the idea of saving data to come back to at a later date, I was mostly just confused as to why it needed to be converted to JSON and couldn't just be stored as a swift file that was all. I'm guessing User Defaults only accepts certain data types. I haven't covered Core Data yet so it is possible that as I become familiar with different formats these things will make more sense. I am still confused by what the "Items" key is for though. I don't see it anywhere else in the program. Is it just a placeholder word? Sorry for all the questions. I think that is the only one that is really bugging me now though. I feel much less confused than I was at the beginning of the post! |
|
Paul had touched upon
Why use JSON in this case? I think it is just to practice encoding and decoding simple JSON, reading into and saving from a simple data structure. JSON is used extensively elsewhere, so this is a taster of what you may encounter if you have to decode / encode JSON externally. The key "Items" is used in this line, to prepare access to the
|
|
Thank you @Greenamberred - yes, that's helpful. I have watched that, I think I just was a bit overwhelmed by the amount of new information in the project, that not everything stuck. In the example he uses the key in the property observer, so I guess that makes sense. I'm thinking maybe the key in the iexpenses is simply used to encode and decode the data and doesn't need to be used anywhere else. Sorry, probably just confusing myself now! |
SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.