Swift App does not see JSON data - use of unresolved identifier - json

I am following the Apple Developer Tutorials to learn swift programming. When I try to implement an animation taught in their tutorial here, I cannot add the JSON data to my project directory properly, hence I cannot produce previews.
My project structure is:
Hiking
Hike,swift
HikeView.swift
HikeGraph.swift
HikeDetail.swift
GraphCapsule.swift
Resources/
hikeData.json
However, I get the error mentioned in the title, use of unresolved identifier. I did another project by again following Apple tutorial and JSON data was inside a folder named Resources, and it is working. How should I import the JSON file, hikeData.json?
Edit:
This JSON file is used as follows:
HikeDetail(hike: hikeData[0])

Please check if you target is checked against that file
Then check if it present here

Related

How to add json data into my xcode project?

I'm trying to make a simple Pokedex which allows a user to switch pokemon's name between English and Japanese using Swift. I found this source on the internet and there are Pokemon names both in English and Japanese in the pokedex.json file and Pokemon images in the Images folder, so I'm planning to use the source, but the problem is I'm not sure how to access the data without endpoint.
Usually, I use the swiftyJOSN and the Alamofire in order to make a request to API and parse the JSON data, however, the source doesn't have an API endpoint. So that is why I'm confused about how to access the data without any GET request.
I guess somehow I need to put the pokedex.json and other image files into my code project and make my code accessible to data (sorry I'm not sure). So, if I'm correct, can anyone tell me how can I import the file to my project? On the other hand, if I'm trying to do something completely wrong, could anyone please tell me about that?
Also, I'd like to know how can I access the data in the JSON file using Swift.
Yes, you can ship a file as a resource inside your app bundle.
Here's how you can do it:
In Xcode, create a group inside your project and give it a descriptive name, for example "Resources".
Download the pokedex.json file from your source.
Drag and drop this pokedex.json file into that group from Finder to Xcode. When the file adding dialog appears, be sure to check the "Copy items if needed" checkbox and that your main target is selected.
In the code, you read the resource like this:
guard let pokedexFilePath = Bundle.main.path(forResource: "pokedex", ofType: "json"),
let pokedexData = FileManager.default.contents(atPath: pokedexFilePath) else
{
// TODO: handle error appropriately
return
}
// Now you can parse pokedexData with SwiftyJSON, Codable or whatever you prefer.
However if you need to ship image files with your app, you may find using an asset catalog (the blue Assets.xcassets folder in your Xcode project) more convenient rather than using the approach I described above. In asset catalog, you can specify different versions of the same image for different screens or interface themes and even have localized versions of the same image if needed. When using an asset catalog, you can create an image in your code by just calling UIImage(named: "your_image_name_in_the_catalog").

Edit JSON File in Scratch 3.0

I would like to know if it is possible to edit the JSON file of Scratch 3.0 project. I downloaded a project, changed the name into a .zip, unzipping it, and opening the JSON file that it left. I did not change any line of the JSON code. I compressed the project and changed its name into sb3; but when I upload it the Scratch 3.0 GUI I got this:
Oops! Something went wrong.
We are so sorry, but it looks like Scratch has crashed. This bug has been automatically reported to the Scratch Team. Please refresh your page to try again.
Make sure the folder structure of the new sb3 matches the original. If you zip the outer folder after extracting instead of the files inside, the folder structure won't be what Scratch expects and it will throw an error.

Correct way of importing data from json in Ionic 2 using an interface

I'm really wondering what's the correct and best-practice way for importing data from a json file in an ionic 2 project so I can show it in a template, for example.
Some solutions I can find use interfaces, some don't. Some implement own small services, some don't. Some use http.get().map().subscribe(), some just use subscribe(). I would really like to know how to do this properly and would be glad if somebody could enlighten me.
From my view the best practice is to use a service and use http get request to get json file from your server. We did the same for getting language json file.
If you want securely get json file then you should put your json file at secure folder rather than public folder and create an api to get json file or json object from server.
Hope it will help

Make Alfresco Web Script show error 500 as JSON rather than HTML

I am writing an Alfresco Web Script that will be consumed by external computers.
When I throw a WebScriptException, I would like the error 500 to come with an error page in JSON, rather than HTML, so that the external computers can parse it. Right now I am getting this:
How to get JSON instead?
I see that some Web Scripts manage to produce error pages as JSON (for error 401 though, so probably before the Web Script actually gets called):
You can create a freemarker dedicated for each status code. If you go to this web page http://docs.alfresco.com/5.2/concepts/ws-component-name.html and scroll to "FreeMarker Templates - Response status", you can see some examples.
Quoting as of 5.2:
Response status code document file names adhere to a naming convention
as defined by the Web Script Framework. The appropriate response
status code template is searched for in the following order:
A template located in the same folder as the web script description document for rendering a specific status code response,
which adheres to the naming convention <web script id>.<http method>.<format>.<status code>.ftl
A template located in the same folder as the web script description document for rendering a response of any status code,
which adheres to the naming convention <web script id>.<http method>.<format>.status.ftl
A package-level template located in the package of the web script but, if not found, is searched for in the parent package hierarchy, up
to the root package for rendering a response of any status code, which
adheres to the naming convention <format>.status.ftl
A template located in the root package for rendering an HTML response for the specific status code, which adheres to the naming
convention <status code>.ftl
A template located in the root package for rendering an HTML response of any status code, which adheres to the naming convention:
status.ftl

How to make an F# JSON type provider sample available for test project?

I am using an F# JSON type provider to create a type from a reference JSON document. The reference document "ReferenceItem.json" is part of the F# library. In addition I have a unit test project which tests the library. I am struggling with making the reference document available for the test project without duplicating it.
No matter how I mark "ReferenceItem.json" in Visual Studio (Content, None, Copy to Output etc.) my test project fails to compile because the statement JsonProvider<"ReferenceItem.json"> expects "Reference.json" to be present in the project source folder at compilation time. Including it as a linked item from the library project doesn't help: it's not copied at compile time to the test source folder. So I need to make a duplicate copy of the file in the test project.
I noticed that in F# projects I can mark files as "DesignData" or "DesignDataWithDesignTimeCreatableTypes", but I wasn't able to figure out how I can use them.
This is a tricky problem - when F# compiler references the library, it will invoke the type provider and so the type provider needs to be able to access the sample.
The easiest solution is to just always copy the sample json file so that it is in the folder from where the application is starting. This is obviously sub-optimal, and so we have another way of handling this using resources.
See the "Using JSON provider in a library" section of the documentation. The idea is that you can embed the sample document as a resource in the library and specify the resource name as an additional parameter:
type WB = JsonProvider<"../data/WorldBank.json",
EmbeddedResource="MyLib, worldbank.json">
This will then load the resource when using the library (but it still needs the file name in the original compilation mode). This is still somewhat experimental, so please open an issue on GitHub if you cannot get it to work!