简体   繁体   English

将JSON数据(从.txt文件获取)存储在数据库中

[英]Store JSON Data (get from .txt file) in Database

I am new at iPhone Development. 我是iPhone开发的新手。

I know how to store JSON data in database. 我知道如何在数据库中存储JSON数据。 But in my case I have .txt file which contain data of JSON . 但就我而言,我有.txt file ,其中包含JSON数据。 This .txt file i get by send request on Server and i got it as .zip file and after i did it as unzip file so after i get it as .txt file with JSON data. 我通过服务器上的发送请求获得此.txt file ,我将其作为.zip文件获取,并在将其作为解压缩文件获取后,因此在将其获取为具有JSON数据的.txt文件之后。 I get this data from .txt file but i dont know how to store it in database.I want to stor this data in DATABASE. 我从.txt文件中获取了这些数据,但是我不知道如何将其存储在database.I中,我想将这些数据存储在DATABASE中。 How cant i do anybody as idea ??? 我怎么不能做任何人的主意???

Data of .txt file of JSON ( Display in Console ) JSON .txt文件的数据( 在控制台中显示

"Ok","last_response_date":"2013-02-05 00:04:14"," region_master ":[{"id":"1","region_name":"Auckland and Kermadec","border_coordinate_file":"","created_date":"2013-02-01 05:22:42","updated_date":"0000-00-00 00:00:00"},{"id":"2","region_name":"Central","border_coordinate_file":"","created_date":"2013-02-01 05:22:42","updated_date":"0000-00-00 00:00:00"},{"id":"3","region_name":"Challenger","border_coordinate_file":"","created_date":"2013-02-01 05:22:57","updated_date":"0000-00-00 00:00:00"},{"id":"4","region_name":"Fiordland","border_coordinate_file":"","created_date":"2013-02-01 05:22:57","updated_date":"0000-00-00 00:00:00"},{"id":"5","region_name":"South East","border_coordinate_file":"","created_date":"2013-02-01 05:23:15","updated_date":"0000-00-00 00:00:00"},{"id":"6","region_name":"Southland","border_coordinate_file":"","created_date":"2013-02-01 05:23:15","updated_date":"0000-00-00 00:00:00"}], “ OK”,“ last_response_date”:“ 2013-02-05 00:04:14”,“ region_master ”:[{“ id”:“ 1”,“ region_name”:“ Auckland and Kermadec”,“ border_coordinate_file”:“ “,” created_date“:” 2013-02-01 05:22:42“,” updated_date“:” 0000-00-00 00:00:00“},{” id“:” 2“,” region_name“: “ Central”,“ border_coordinate_file”:“”,“ created_date”:“ 2013-02-01 05:22:42”,“ updated_date”:“ 0000-00-00 00:00:00”},{“ id” :“ 3”,“ region_name”:“挑战者”,“ border_coordinate_file”:“”,“ created_date”:“ 2013-02-01 05:22:57”,“ updated_date”:“ 0000-00-00 00:00 :00“},{” id“:” 4“,” region_name“:” Fiordland“,” border_coordinate_file“:”“,” created_date“:” 2013-02-01 05:22:57“,” updated_date“: “ 0000-00-00 00:00:00”},{“ id”:“ 5”,“ region_name”:“东南”,“ border_coordinate_file”:“”,“ created_date”:“” 2013-02-01 05 :23:15“,” updated_date“:” 0000-00-00 00:00:00“},{” id“:” 6“,” region_name“:” Southland“,” border_coordinate_file“:”“,” created_date“ “:” 2013-02-01 05:23:15“,” updated_date“:” 0000-00-00 00:00:00“}],
" region_kml_files ": region_kml_files ”:
[{"id":"4","region_id":"2","kml_files":"Sample.kml","created_date":"2013-01-25 09:04:58","updated_date":"0000-00-00 00:00:00"}] [{“ id”:“ 4”,“ region_id”:“ 2”,“ kml_files”:“ Sample.kml”,“ created_date”:“ 2013-01-25 09:04:58”,“ updated_date”:“ 0000-00-00 00:00:00“}]

EDIT : 编辑:


I already got data from .txt file 我已经从.txt file了数据


please help me.. 请帮我..
Thanks :) 谢谢 :)

You can use: 您可以使用:

 NSString *jsonString  = [NSString stringWithContentsOfFile:yourFilePath encoding:UTF8StringEncoding error:nil];

Here: 这里:

yourFilePath is a NSString which contains the exact path to the .txt file. yourFilePath是一个NSString,其中包含.txt文件的确切路径。

You get the file content in jsonString variable, you need to save that string to database. 您在jsonString变量中获取文件内容,需要将该字符串保存到数据库。

Refer NSString class for more 有关更多信息,请参见NSString

I don't know where you are having trouble, but it seems like you haven't gotten a JSON object from your string yet, so I will let you know how to do that. 我不知道您在哪里遇到麻烦,但是看来您还没有从字符串中获取JSON对象,因此我将告诉您如何执行此操作。 If you need to know how to create and store data in a database, then you need to make a new question and say that you got JSON data but are having trouble storing it into a database (at least make an effort to try to do it though). 如果您需要了解如何在数据库中创建和存储数据,则需要提出一个新问题,并说您拥有JSON数据,但是在将其存储到数据库时遇到了麻烦(至少要努力尝试做到这一点)虽然)。

You can use NSJSONSerialization to get an object out of your string (the NSData version of your string, at least). 您可以使用NSJSONSerialization从字符串中获取对象(至少是字符串的NSData版本)。 It will look like this: 它看起来像这样:

NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:yourData options:kNilOptions error:nil]; 

If it doesn't work then your JSON is not valid. 如果不起作用,则您的JSON无效。 Validate it at http://www.jsonlint.com http://www.jsonlint.com上进行验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM