-1

I have to encode string in PHP using JSON. And I have a problem with finding anywhere examples of encoding strings. Everybody encodes arrays. Do you have any example of this in PHP and in obj c of decoding?

2
  • Did you read this? Commented Sep 19, 2014 at 14:29
  • Yes but it's array in this string Commented Sep 19, 2014 at 14:30

2 Answers 2

0

Have u tried this one .

<?php

  $stringSingleElement = new array($yourstringdata) ;


 json_encode(stringSingleElement[0]);

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Hi , mickey how about trying place that string into a single element of an array then access it like $thatarray[0] thing ..?
0

Generally speaking when you want to json encode something in PHP, you can use json_encode. Your root object must be an array (or dictionary) in order to produce a valid json string. Note also that json_encode doesn't complain when you use it directly with strings (but it doesn't produce a valid json string).

echo json_encode(array('a' => 'test1', 'b' => 'test2'));
// {"a":"test1","b":"test2"}

echo json_encode(array('c'));
// ["c"]

echo json_encode('d');
// "d"

Objective-c code

// Let's assume that jsonString is a NSString containing {"a":"test1","b":"test2"}
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"dictionary string: %@", dictionary[@"a"]); // Output: test1

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.