3

I Want to make a string like below

contentlist=["{\"id\":\"id1\"}"]

How can i achieve this ?

I tried with NSString method stringWithFormat:@"contentlist=[\"%@\"] but it has \ character when you copy that string and paste it into textedit or in stickies or anywhere.

if you print this line that it will display like contentlist=["{"id":"id1"}] that i dont want.

Edited :

Here is my code..

NSString *stringUrl = <MY SERVER URL STRING>
NSString *param = [NSString stringWithFormat:@"contentlist=[%c%@%c]",34,header,34];

NSData *postData = [param dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];

NSError *error;
NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:stringUrl parameters:nil error:&error];
req.timeoutInterval= 40.0;

[req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[req setValue:postLength forHTTPHeaderField:@"Content-Length"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[req setHTTPBody:postData];

And my header value is..

{\"id\":\"21ca7309\",\"indexVersion\":0,\"modified\":1501694202014,\"created\":1501691797232,\"modifiedBy\":\"42bebd87be6ddd4a\",\"name\":\"abcd\"}

How my param value look like in debug mode..

@"contentlist=[\"{\"id\":\"21ca7309\",\"indexVersion\":0,\"modified\":1501694202014,\"created\":1501691797232,\"modifiedBy\":\"42bebd87be6ddd4a\",\"name\":\"abcd\"}\"]

And how I need to pass is like :

contentlist:["{\"id\":\"21ca7309\",\"indexVersion\":0,\"modified\":1501694202014,\"created\":1501691797232,\"modifiedBy\":\"42bebd87be6ddd4a\",\"name\":\"abcd\"}"]
11
  • 1
    you wan't to convert json string to array?? Commented Aug 3, 2017 at 6:36
  • I am not getting what you want from your question. Commented Aug 3, 2017 at 6:39
  • What is 34 here ? Commented Aug 3, 2017 at 7:13
  • @MikeAlter that is a ascill code equivalent to "...I tried with ascii character also.. you can check in this url theasciicode.com.ar/ascii-printable-characters/… Commented Aug 3, 2017 at 7:16
  • You need to pass it in to array ? , then use like NSArray *arr = @[header] , and then try to use dataUsingEncoding on array Commented Aug 3, 2017 at 7:32

1 Answer 1

3

You don't required to use stringWithFormat

as you mentioned you have NSData of JSON if you don't have that then convert it using

NSError *error;
NSData *data =  [NSJSONSerialization dataWithJSONObject:yourJSONO options:NSJSONWritingPrettyPrinted error:&error];

Don't forgot to check error before use :)

you can easily get string from that with below code

NSString *str = [[NSstring alloc] initWithData:data encoding:NSUTF8StringEncoding]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.