1

I write a little snippet of usual code but found that my code don't return hex data from server with this line of code:

let currentData = try! Data(contentsOf: fullURL!)
print("currentData=", currentData)

And the output:

currentData= 24419 bytes

I tried to use Leo's comment link:

stackoverflow.com/q/39075043/2303865

I got something hex data without spaces, and validator (http://jsonprettyprint.com) can't recognise it and returns null.

wm.p1us
  • 2,019
  • 2
  • 27
  • 38
  • You were probably (ab)using the fact that the description method of NSData returns a "hex dump", which is no longer true for Data. But the data is there. Why do you think it isn't, and what do you actually want to achieve? – Martin R Aug 27 '16 at 06:36
  • @MartinR I just want to get data and then use JSONSerialization to get it into JSON format. – wm.p1us Aug 27 '16 at 06:59
  • And `JSONSerialization.jsonObject(with: currentData)` does not work? What result do you get and what do you expect? – Martin R Aug 27 '16 at 07:02
  • Serialisation is ok. But want to see output and try to print currentData (with Data()) but console shows kind of garbage. – wm.p1us Aug 27 '16 at 07:05
  • http://stackoverflow.com/q/39075043/2303865 – Leo Dabus Aug 27 '16 at 07:06
  • @LeoDabus: This looks more like an XY-problem to me. If OP wants to deserialize JSON data then the hex dump is irrelevant. – Martin R Aug 27 '16 at 07:09
  • So all he needs is to convert the data to String utf8 – Leo Dabus Aug 27 '16 at 07:11
  • @wm.p1us: Your update does not help. There is no `print` statement in your code, therefore we cannot answer why you don't see any print data in the console. – Martin R Aug 27 '16 at 07:14
  • @wm.p1us: Can you remove the unrelated stuff? Have you checked if the link that Leo pointed to answers your question? What does `print("currentData=", currentData)` show? – Martin R Aug 27 '16 at 07:18
  • Can you post your actual code? As Martin already mentioned you need to use JSONObjectWithData to serialize your data – Leo Dabus Aug 27 '16 at 07:35
  • I can serialise it so I got correct JSON. But I want to understand why hex data which I can get with your link returns nil in jsonprettyprint.com – wm.p1us Aug 27 '16 at 07:37
  • You can print the returned data using `String(data: currentData, encoding: .utf8)` – Leo Dabus Aug 27 '16 at 07:39
  • Your first link make hex data in the console appear. So I think that was correct way to do things. But now I try to figure out why that hex data I can't convert to normal JSON format with online services. – wm.p1us Aug 27 '16 at 07:45
  • @LeoDabus I know this is just test new classes code. It's unsafe. – wm.p1us Aug 27 '16 at 07:46
  • @wm.p1us: http://jsonprettyprint.com requires the JSON as a string, not as hex data. It wouldn't work with the hex output from NSData either. – Martin R Aug 27 '16 at 07:51
  • Omg I understand it at the same time you wrote. Have to sleep :( Please wrote some answer about console output and I accept it. – wm.p1us Aug 27 '16 at 07:53

1 Answers1

5

Let's try to sort out the different issues here and summarize the above comments.

The description method of Data prints only a short summary "NNN bytes", and not a hex dump as NSData did:

let o = ["foo": "bar"]
let jsonData = try! JSONSerialization.data(withJSONObject: o)

print(jsonData) // 13 bytes

You can get a hex dump by bridging to NSData (source):

print(jsonData as NSData) // <7b22666f 6f223a22 62617222 7d>

or by writing an extension method for Data (How to convert Data to hex string in swift).

But that is actually not the real problem. The JSON validator needs the JSON as a string, not as a hex dump (source):

print(String(data: jsonData, encoding: .utf8)!) // {"foo":"bar"}

And to de-serialize the JSON data into an object you would need none of the above and just call

let obj = try JSONSerialization.jsonObject(with: jsonData)
Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382