JSON integration with Swift

Asked

Viewed 342 times

1

I wanted to know how I pass a JSON with my URL to Swift.

I have one as an example: http://peps.com.br/pips/teste2.json and if someone can do the start of my JSON in the Swift language would already help me enough because I’m not getting out of place.


then , in my company we have to integrate a JSON that I passed the link in the question for you to see and that make appear in the compiler of the XCODE with the SWIFT language the information of this JSON , but it’s complicated , I wanted at least someone to do the first part of it for me to continue , because it hasn’t opened my head widely how to integrate this !

  • Rafael, can you add more details? How so "do JSON on Swift"? Do you want to generate or interpret JSON? Could you [Edit] and supplement the question please? N! I don’t know anything about Swift, but maybe to interpret you can even use weak language typing, and simply store JSON in a variable. See http://owensd.io/2014/06/21/json-parsing-taketwo.html

3 answers

2

Another solution:

Try this on your Playground from Xcode 6.1.1. Playground is quick to test, then you migrate to your project.

import Foundation
let jsonObject: [AnyObject] = [
  ["name": "João", "age": 20],
  ["name": "Pedro", "age": 45],
]
func JSONStringify(jsonObj: AnyObject) -> String {
  var e: NSError?
  let jsonData: NSData! = NSJSONSerialization.dataWithJSONObject(
    jsonObj,
    options: NSJSONWritingOptions(0),
    error: &e)
  if e != nil {
    return ""
  } else {
    return NSString(data: jsonData, encoding: NSUTF8StringEncoding)
  }
}
let jsonString = JSONStringify(jsonObject)
println(jsonString)

Works in the Mavericks and in the Yosemite

This way it works because you are delegating the work of serialization to class NSJSONSerialization of the standard library

Meaning, your Swift code is just a Wrapper.

Deserialization can also be done in an analogous way.

0

Rafael, in one of our projects, which is a very complex app, we use the Swiftyjson. Its use is as simple as

let json = JSON(data: dataFromNetworking)

Then Voce picks up the values by subscript:

let userId = json["compromissos"][0]["usuario_id"].intValue
let userName = json["compromissos"][0]["usuario_nome"].stringValue

This is very simplified, because Voce will probably use arrays:

let appointments = json["compromissos"].arrayValue

To make the requests we use the AFNetworking, but Voce can use Alamofire as it is on the Switfyjson page.

0

Rafael, I tried to create an example to try to solve your doubt using another possibility Codable, this json represents an object that we want to use in our project, in the second image is how we will represent the object in the code. Using Encodable, you can create a json from a Swift object and Decodable is used to convert json to an object in Swift. To finish, as your task is only to carry out the decoding you must take the Date obtained in your request and try to convert-Lô to your struct.

inserir a descrição da imagem aquiinserir a descrição da imagem aquiinserir a descrição da imagem aqui inserir a descrição da imagem aquiinserir a descrição da imagem aqui

If you still have doubts, this is the official documentation link -https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types

Browser other questions tagged

You are not signed in. Login or sign up in order to post.