Getting inside of arrays in json / Alamofire [Swift] - json

I am using Alamofire for getting json of: https://randomuser.me/api/
Is there any simple way to get inside those arrays in Swift? I was trying like:
Alamofire.request(.GET,"https://randomuser.me/api/").responseJSON{
(response) -> Void in
if let json = response.result.value{
print(json["results"]["user"]["name"])
}
Thanks!

If you don't mind using a library, I'd highly suggest SwiftyJSON.
It's good to note that this library is not required and parsing JSON can be done with NSJSONSerialization.

In Swift this is currently possible only with an extension / library.
Some libraries with this feature:
SwiftyJSON
In your case, maybe a perfect solution: SwiftyJSON and Alamofire combined
Otherwise you'd need to use the NSJSONSterilization and checking each variable with safe unwrapping.

Related

It is necessary to make import json in html

please help me. How import json message to html
https://slack.com/api/channels.history?token=xoxp-60475805059-60530043287-306411842247-c6e3bf60bc947e3da7250883dcfdc54a&channel=C538LFAQK&count=1&pretty=1
If it is a ajax call you need to call using the jquery, you need to call using the <object>.<attribute> like man.name.
IF you want to call using php, you need to decode json array using json_decode($jsonArray). The function is to convert json to array.
you can call using $object[attribute].
JSON decode using php: How do I extract data from JSON with PHP?
JSON decode using jquery: jQuery JSON Decode ( PHP to Javascript)

scala akka-http - complete get request with a pre-built json object and stream it

I am trying to complete a get request by returning a pre-built JsonArray, and also find a way to stream it. I can easily complete the request without any errors and return Json if I convert the JsonArray to string, like so:
get {
path("getJsonData") {
parameterMap {
params =>
complete(HttpEntity(ContentTypes.`application/json`, myJsonArray.toString))
}
}
}
However, I would like to avoid converting to string, and be able to stream the JsonArray, because the resulting JsonArray can be very large in some cases.
JsonArray is created from scratch from individual JsonObjects, and I do not use case classes, so I could not use the standard approaches I found in the documentation.
I am new to Akka Http and not sure if there is a simple way to solve this problem, would appreciate some help.
With the below you will be streaming each element of your JSON array in a separate HTTP chunk.
complete(HttpEntity(ContentTypes.`application/json`,
Source(myJsonArray.elements).map(j ⇒ ByteString(j.prettyPrint))))
Note that:
The choice of prettyPrinting the JSON can be revisited to fit your needs.
You can adjust the size of your frame by batching elements together using the Akka Streams API.

Need a JSON parser for Unity3d

I need to deserialize some JSON objects. I tried to use Tiny-json library, but it's too slow. I tried to use Newtonsoft.Json, but it fails in webplayer with this error:
MissingMethodException: Method not found: 'System.Collections.ObjectModel.KeyedCollection.
What JSON parser do you recommend?
You can try one of these open source solutions:
https://github.com/jacobdufault/fullserializer
https://github.com/mtschoen/JSONObject (https://www.assetstore.unity3d.com/en/#!/content/710) I am using this one most of the times, it's versbose but does its job well, not sure about performance however
Or go with paid ones:
https://www.assetstore.unity3d.com/en/#!/content/11347
Unity 5.3 added Native support of Json Serializer. It is faster than others.
JsonUtility.ToJson to convert a class to Json.
JsonUtility.FromJson to convert Json back to class.
For complete example and information regarding json arrays, see
Serialize and Deserialize Json and Json Array in Unity

Best way to deserialize JSON dictionary with swift?

I have an array column of parse dictionary objects that are in JSON. I am trying to find the best way to deserialize it And then use the information on a tableview Cell. Ive never used JSON before so thanks for the help!
Here is the Column..
so far with this line of code i can print all the containing objects, but I haven't been able to deserialize it yet.
let history: AnyObject? = poolHistory.valueForKey("serviceHistory")
print("\(history)")
SwiftyJSON is a very good library, more :
https://github.com/SwiftyJSON/SwiftyJSON

Parse Simple Json Array in Play Framework

I'm sure there is a simple way to do this, but I haven't found a way to do it. How do I go about parsing a simple json array: [1] as a List[Integer] or List[String] with the play framework Json Library in scala?
All of the examples I have seen in the documentation assume you are working with an object and are accessing an attribute in the object. When I try to parse the json I get an error:
jsonResult.as[List[String]]
JsError(List((,List(ValidationError(error.expected.jsarray,WrappedArray())))))