2014-09-19 90 views
-1

我必須encode字符串PHP使用JSON。我在任何地方找到編碼字符串的例子都有問題。每個人都編碼數組。你有沒有在PHP和解碼對象的任何例子?PHP + OBJ C Json編碼字符串

+0

您是否閱讀過[this](http://stackoverflow.com/questions/11500366/encoding-string-to-json)? – Compass 2014-09-19 14:29:02

+0

是的,但它是這個字符串中的數組 – Mikey 2014-09-19 14:30:43

回答

0

一般來說,當你想用json編碼PHP中的東西時,你可以使用json_encode。您的根對象必須是數組(或字典)才能生成有效的json字符串。 還請注意,json_encode不直接使用字符串(但它不會生成有效的json字符串)。

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代碼

// 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 
0

具有u嘗試這一個。

<?php 

    $stringSingleElement = new array($yourstringdata) ; 


json_encode(stringSingleElement[0]); 

?> 
+0

我正在使用回聲json_encode($ string) – Mikey 2014-09-19 14:31:46

+0

嗨,米奇如何嘗試將該字符串放入數組的單個元素,然後像$ thatarray [0]的東西.. ..訪問它? – danielad 2014-09-19 14:33:03

+0

我會盡力,謝謝;) – Mikey 2014-09-19 14:35:17