Comments (4)
- You should be using the
<|
operator for retrieving values from JSON:
let id: Int = j <| "id"
let text: String = j <| "text"
- You can get values of the type
[String: JSONValue]
with a simple custom parser:
func getDictionary(j: JSONValue) -> [String: JSONValue]? {
switch j {
case .let JSONObject(o): return o
default: return .None
}
}
That being said, we already have a built-in syntax for traversing dictionaries for nested keys:
let nested: String = j <| ["path", "to", "nested", "key"]
I highly suggest reading as much of the README as possible. I know it's long, but there's a ton of info in there.
I'm going to close this issue, but please feel free to keep asking questions if any of this still doesn't make sense.
from argo.
As stated above, you should use the built in conventions for retrieving the data from the JSON. However, number 1 would be possible (although not recommended). You might want to read up on Optionals and their uses ... here is a little explanation to what you were seeing:
For that particular line: let id: Int = j["id"]?.value()
, the j["id"]
will return a JSONValue?
which you can then optionally unwrap and call value()
. value()
returns the type you are requesting wrapped in an optional. In this case you are requesting an Int
so it will return Int?
. You cannot assign an optional value to a non optional variable so it fails with that error message you are seeing. The solution would be to explicitly unwrap (always a bad idea) the optional to get the Int
. However, throwing an !
at the end of this statement doesn't help because the j["id"]?
could still fail and return .None
. To get directly at the value wrapped by the optional, the statement would look like this: let id: Int = j["id"]!.value()!
... again you shouldn't do this. Instead, use the value as an optional, let id: Int? = j["id"]?.value()
or unwrap it using if-let
syntax like so:
if let id = j["id"]?.value() as Int? {
println(id)
}
That being said, still try to use Argo as intended as most of this value stuff will be deprecated in future versions.
from argo.
Thank you @tonyd256 and @gfontenot for the explanation, I now have a much better understanding. One last question, can you explain this error message for:
if let text = j <| "text" {
println(text)
}
error: cannot invoke '<|' with an argument list of type '(JSONValue, StringLiteralConvertible)'
if let text = j <| "text" {
~~^~~~~~~~~
When the correct code is:
if let text = j <| "text" as String? {
println(text)
}
from argo.
This is because the <|
operator is relying on type inference meaning that you need to tell it what type you want through a cast like in your second example. The error message is kind of misleading but basically Swift doesn't know what type you want so it's confused. By casting the variable or the result of the expression, you are telling the operator which type to try and return.
from argo.
Related Issues (20)
- error: no such module 'Runes' HOT 7
- Problem with Decodable on xcode 9 HOT 3
- RFC: Renaming the Decodable protocol
- How to decode a "Dictionary" to "[Model]"? HOT 3
- Parse nested array failed HOT 5
- Parse nested model fails when adding a specific key HOT 2
- How to decode dynamic keys HOT 2
- RFC: Renaming (or removing) the `<|` family of operators HOT 5
- Decode type 'Any' HOT 2
- RFC: Make Swift.Decodable conform to Argo.Decodable in Swift 4.1 HOT 4
- Convert all wrong type return JSON to specific type HOT 6
- Decoding the same object with different keys HOT 6
- How to parse an empty dictionary object in Argo HOT 1
- [Question] Array decoding HOT 4
- Using pure can't compile using Swift 3 HOT 2
- Decodable' is ambiguous for type lookup in this context (Swift4) HOT 1
- Parsing a json array with strings and objects intermixed HOT 2
- Xcode 11.0 compatibility issue HOT 3
- Xcode 11.2.1 Binary operator '<^>' cannot be applied to operands of type HOT 7
- Upgrade to version 5.0.0 blocked due to Ogra library HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from argo.