Question

I'm trying to do a POST to a RESTful Web API and having issues with the format on the httpBody. NSJSONSerialization is creating this:

{
  "UserId" : "ThisIsMe",
  "Locations" : "[\n  \"1\"\n]",
  "TypeOfValue" : "3",
  "SomeTypeID" : 22,
  "Title" : "asdfadsf",
  "Description" : "Description"
}

But what I need is this:

{
  "UserId" : "ThisIsMe",
  "Locations" : ["1"],
  "TypeOfValue" : "3",
  "SomeTypeID" : 22,
  "Title" : "asdfadsf",
  "Description" : "Description"
}

In order to put this together "as is" I had to take an NSArray run NSJSONSerialization on it then append that to an NSDictionary and then call NSJSONSerialization on that. So there is a problem obviously with the double serialization, but if I pass in the array with the NSDictionary it fails...help please.

Was it helpful?

Solution

You should have an NSDictionary whose keys are all strings and whose values are NSString, NSNumber or NSArray (containing NSString) instances. Then, use NSJSONSerialisation to convert that dictionary representation into a JSON data representation and you will get what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top