0

I need to form PUT request like this:

First example created on Java (android):

public JSONObject putGameData(String answer_id) {
    JSONObject jsonObject = new JSONObject();

    if(myAnswersArray.contains(new String(answer_id))){
        for(int i = 0; i < myAnswersArray.size(); i++)
            if(answer_id.equals(myAnswersArray.get(i)))
                myAnswersArray.remove(i);

    } else {
        myAnswersArray.add(answer_id);
    }

    JSONArray jsonArray = new JSONArray(myAnswersArray);
    try {
        jsonObject.put("answer_id", jsonArray);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return jsonObject;
}

PARAMETERS: {"answer_id":["a104fccc555a60ea8e437d8b7f11dd12","ad4277811a26ebea5d72d09a164531ee","5a1cc90dc9b51d6f8793759cab11728f"],"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJzdWIiOiIx}

On iOS app I send parameters like this and it's not correct. My parameters are with \n and other whitespaces...

PARAMETERS: {"{\"answer_id\":":{"\"20052e3c88ef5c16d9ae0ac0d86ffbca\",\"2600c200e39815352ec1c7d602d67d25\"":""},"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJzdWIiOiIxMzUiLCJpc3MiOiJodHRwOlwv"}

I did:

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:multipleAnswersArray options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSASCIIStringEncoding];
[post setObject:multipleAnswersArray forKey:@"answer_id"];

and then perform put data:

NSData *postData = [NSJSONSerialization dataWithJSONObject:post options:0 error:&error];

How to make correct format like on first example?

lost.in.code
  • 17
  • 10
  • 1
    Don't use `NSJSONWritingPrettyPrinted` that's what adds `\n` – Larme Dec 06 '16 at 16:58
  • Also, as written, your "request parameters" are not JSON, what's after "PARAMETERS:" is, but that doesn't include "PARAMETERS:". – Larme Dec 06 '16 at 17:00
  • @Larme I have excluded NSJSONWritingPrettyPrinted but it's the same situation. :( First variant is created by android, I will add the code in header of my question for better understanding. Maybe it will help and someone knows how to write the same on Objective C. – lost.in.code Dec 06 '16 at 18:06
  • Why do you `setObejct: multipleAnswersArray` instead of `setObject: jsonString`? – Ryan Dec 06 '16 at 18:28
  • This is not `\n`. Just escape characters. – Ryan Dec 06 '16 at 18:35
  • Possible duplicate of [how to prevent NSJSONSerialization from adding extra escapes in URL](http://stackoverflow.com/questions/19651009/how-to-prevent-nsjsonserialization-from-adding-extra-escapes-in-url) – Ryan Dec 06 '16 at 18:35
  • @Ryan when I use "jsonString" - the same story :( – lost.in.code Dec 06 '16 at 21:16

1 Answers1

0

Thank you all for helping but I found a solution. I use AFNetworking and just put my array to parameters and it's working very well ;)

lost.in.code
  • 17
  • 10