2013-08-01 65 views
0

如何在iPhone中使用Plist文件創建父和子我有一個json對象。這個對象來自web服務。 我想嘗試使用plist文件創建一個樹結構。我可以通過編程創建plist嗎?使用json對象

我可以通過編程創建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" 
       } 
       ] 
      } 
      ] 
     } 
+0

對不起編程 –

+0

沒有工作或者沒有找到plist文件。通過在NSLog – Warewolf

回答

2

是爲什麼不只是在NSDictionary中獲取JSON對象並將其寫入由您定義的路徑的文件。

由於json是NSDictionary對象。

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

粘貼下面的代碼到viewDidLoad中,你會到這裏來定義你的plist文件路徑時

NSString *[email protected]"{\"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]; 
+0

中打印它的值,在filePath中看到plist文件,它將自動創建所有子節點。 –

+0

是的,它會創建 – Warewolf

+0

這實際上由文檔覆蓋得很好。搜索會產生數十個例子。 – tooluser

相關問題