我能以编程方式创建plist吗?使用json对象

时间:2013-08-01 11:31:56

标签: iphone ipad

如何在iPhone中使用Plist文件创建父级和子级我有一个json对象。这个对象来自Web服务。 我想尝试使用plist文件创建树结构。

我能以编程方式创建plist文件吗?使用json对象

         {
          "component":[
            {
              "id":1006,
              "name":"Image test 2",
              "child":[
                {
                  "id":1101,
                  "name":"component score mcq"
                },
                {
                  "id":1105,
                  "name":"component we score"
                },
                {
                  "id":1095,
                  "name":"a"
                },
                {
                  "id":1103,
                  "name":"component scq score"
                },
                {
                  "id":1134,
                  "name":"Hi this is my first blog from lesson"
                },
                {
                  "id":1106,
                  "name":"abc"
                },
                {
                  "id":1102,
                  "name":"component fitb score"
                },
                {
                  "id":1096,
                  "name":"testkrupsvideo"
                },
                {
                  "id":1104,
                  "name":"component either score"
                },
                {
                  "id":1099,
                  "name":"krupsblog updated"
                },
                {
                  "id":1224,
                  "name":"krupsforum updated"
                },
                {
                  "id":1127,
                  "name":"reading parent topic test"
                },
                {
                  "id":1093,
                  "name":"abc"
                },
                {
                  "id":1128,
                  "name":"ankit parent topic"
                },
                {
                  "id":1129,
                  "name":"test topic for component",
                  "child":[
                    {
                      "id":1228,
                      "name":"krupsud"
                    },
                    {
                      "id":1253,
                      "name":"06-12-2012 we updated"
                    },
                    {
                      "id":1130,
                      "name":"topic reading"
                    },
                    {
                      "id":1251,
                      "name":"06-12-2012 fitb"
                    },
                    {
                      "id":1225,
                      "name":"krupsvideo updated"
                    },
                    {
                      "id":1249,
                      "name":"06-12-2012 mcq"
                    },
                    {
                      "id":1250,
                      "name":"06-12-2012 scq"
                    },
                    {
                      "id":1226,
                      "name":"krupsaudio updated"
                    },
                    {
                      "id":1227,
                      "name":"krupsflash updated"
                    },
                    {
                      "id":1252,
                      "name":"06-12-2012 eo"
                    }
                  ]
                },
                {
                  "id":1124,
                  "name":"First dlr wiki for positive test",
                  "child":[
                    {
                      "id":1125,
                      "name":"Component wiki"
                    }
                  ]
                },
                {
                  "id":1126,
                  "name":"bhushan reading dlr"
                }
              ]
            }
          ]
        }

1 个答案:

答案 0 :(得分:2)

是的,为什么不在NSDictionary中获取JSON对象并在您定义的路径上写入文件。

由于json是NSDictionary对象。

NSString *filePath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/myfile.plist"];
[dictionary writeToFile:filePath atomically:YES];

将以下代码粘贴到viewDidLoad,您将获得此处定义的filePath的plist

NSString *jsonString=@"{\"component\":[ { \"id\":1006, \"name\":\"Image test 2\", \"child\":[ { \"id\":1101, \"name\":\"component score mcq\" }, { \"id\":1105, \"name\":\"component we score\" }, { \"id\":1095, \"name\":\"a\" }, { \"id\":1103, \"name\":\"component scq score\" }, { \"id\":1134, \"name\":\"Hi this is my first blog from lesson\" }, { \"id\":1106, \"name\":\"abc\" }, { \"id\":1102, \"name\":\"component fitb score\" }, { \"id\":1096, \"name\":\"testkrupsvideo\" }, { \"id\":1104, \"name\":\"component either score\" }, { \"id\":1099, \"name\":\"krupsblog updated\" }, { \"id\":1224, \"name\":\"krupsforum updated\" }, { \"id\":1127, \"name\":\"reading parent topic test\" }, { \"id\":1093, \"name\":\"abc\" }, { \"id\":1128, \"name\":\"ankit parent topic\" }, { \"id\":1129, \"name\":\"test topic for component\", \"child\":[ { \"id\":1228, \"name\":\"krupsud\" }, { \"id\":1253, \"name\":\"06-12-2012 we updated\" }, { \"id\":1130, \"name\":\"topic reading\" }, { \"id\":1251, \"name\":\"06-12-2012 fitb\" }, { \"id\":1225, \"name\":\"krupsvideo updated\" }, { \"id\":1249, \"name\":\"06-12-2012 mcq\" }, { \"id\":1250, \"name\":\"06-12-2012 scq\" }, { \"id\":1226, \"name\":\"krupsaudio updated\" }, { \"id\":1227, \"name\":\"krupsflash updated\" }, { \"id\":1252, \"name\":\"06-12-2012 eo\" } ] }, { \"id\":1124, \"name\":\"First dlr wiki for positive test\", \"child\":[ { \"id\":1125, \"name\":\"Component wiki\" } ] }, { \"id\":1126, \"name\":\"bhushan reading dlr\" } ] } ] }";

NSData* data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *dictionary=[[NSDictionary alloc] initWithDictionary:[NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: nil]];

NSString *filePath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/myfile.plist"];
[dictionary writeToFile:filePath atomically:YES];