如何在ios中将此字符串转换为json或字典 [英] how to convert this string to json or dictionary in ios

查看:251
本文介绍了如何在ios中将此字符串转换为json或字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我厌倦了链接,但那些在我的情况下不起作用。

I tired with links but those are not working in my case.

任何人都可以建议我如何将此字符串转换为json或字典。

Can anyone suggest me on how to convert this string to json or dictionary.

此字符串包含一个包含字符串数组的字典。

This string is having a dictionary with an array of strings.

NSString *temp=@"{\n    name =     {\n        dob = \"\";\n        age = \"61\";\n        family =         (\n                        {\n                location = location;\n                mobile = mobile;\n            }\n        );\n    };\n}";

这里的家庭是一个数组。

Here family is an array.

推荐答案

我得到了解决方案。

首先我尝试了你的代码

NSString *temp=@"{\n    name =     {\n        dob = \"\";\n        age = \"61\";\n        family =         (\n                        {\n                location = location;\n                mobile = mobile;\n            }\n        );\n    };\n}";

我的输出结果是

null

然后我更改了JSON格式。

Then I changed the JSON format.

所以现在你的代码应该是

So now your code should be

NSString *strJson=@"{\"name\":{\"dob\":88,\"age\":61},\"family\" : [{\"location\":\"us\",\"mobile\":\"mobile\"}]}";
NSData *data = [strJson dataUsingEncoding:NSUTF8StringEncoding];
id jsonOutput = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",jsonOutput);

打印结果是

{
 family =     (
            {
        location = us;
        mobile = mobile;
    }
);
name =     {
    age = 61;
    dob = 88;
};
}

当你将字符串转换为json时,你必须记住

You have to remember that when you convert string to json

\n must be \"value\"
= must be :
( must be [
number must be 100(or anything else)




一个对象是一个无序的一组名称/值对。一个对象以{(左括号)开始
,以}结尾(右括号)。每个名称是
后跟:(冒号),名称/值对是以
(逗号)分隔。

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).


数组是一个有序的值集合。数组以[
(左括号)开头并以]结尾(右括号)。数值为用(逗号)分隔

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

值可以是双引号中的字符串,或数字,或true或
false或null,或对象或数组。这些结构可以是
嵌套。

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.


字符串是零个或多个Unicode字符的序列,用
双引号括起来,使用反斜杠转义。字符表示为
单个字符串。字符串非常类似于C或Java
字符串。

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.


除了不使用八进制
和十六进制格式之外,数字非常类似于C或Java数字。 / p>

A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.

< img src =https://i.stack.imgur.com/8atUj.gifalt =在此处输入图像说明>

最后


可以在任何一对令牌之间插入空格。除了一些
编码细节,完全描述了这种语言。

Whitespace can be inserted between any pair of tokens. Excepting a few encoding details, that completely describes the language.

我得到了这个有用的数据来自这个

I got this useful data from this

这篇关于如何在ios中将此字符串转换为json或字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆