- 最后登录
- 2019-12-2
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 34660
- 纳金币
- 38268
- 精华
- 111
|
1将从服务器端下载的JSON数据写入到本地的txt文件中
2从txt中读取此内容
这个cs文件写:
void actFunction(string[] content)
{
print("Activity has got:");
Debug.Log("content0:"+content[0]);
Debug.Log("content1:"+content[1]);
Debug.Log("content2:"+content[2]);
Debug.Log("content3:"+content[3]);
Debug.Log("content4:"+content[4]);
//save into database
#if UNITY_IPHONE
Path=PlayerData.sqlpathOfIphoneSQL;
#endif
#if UNITY_ANDROID
Path=PlayerData.sqlpathOfAndroidSQL;
#endif
#if UNITY_EDITOR
Path=PlayerData.sqlpathOfEditorSQL;
#endif
string filepath=Path+"json.txt";
File.WriteAllText(filepath,content[4]);
}
另一个cs文件读
void Start ()
{
//save into database
#if UNITY_IPHONE
Path=PlayerData.sqlpathOfIphoneSQL;
#endif
#if UNITY_ANDROID
Path=PlayerData.sqlpathOfAndroidSQL;
#endif
#if UNITY_EDITOR
Path=PlayerData.sqlpathOfEditorSQL;
#endif
string filepath=Path+"json.txt";
string filecontent = File.ReadAllText(filepath);
act = JsonMapper.ToObject(filecontent);
//解析json数据
DataParses();
}
//解析:用到Litjson.dll
void DataParses()
{
for(int i=0;i<act.Count;i++)
{
Debug.Log(act[i]["title"]+ "-"+ act[i]["content"]);
}
//将载入的内容在界面上呈现出来
loadDataToView(act.Count);
}
|
|