本文目錄一覽:
json數據怎麼通過php存入資料庫
JSON在php中本質是字元串,直接存儲就行了。
看你的圖片,並不是一個JSON,而是一個數組,所以需要先將數組轉碼為JSON,再進行存儲。
// 使用內置函數:json_encode();
$data = array();
$jsonString = json_encode($data);
如何從本地 讀取json文件 並用字典存儲起來
Unity 保存Json數據到本地文件(字典)
一、先導入Json 解析庫;
二、開始代碼的編寫;
[csharp] view plain copy
//命名空間
using System.IO;
using System.Collections.Generic;
using LitJson;
[csharp] view plain copy
//相關變數聲明:
private static string mFolderName;
private static string mFileName;
private static Dictionarystring, string Dic_Value = new Dictionarystring, string();
private static string FileName {
get {
return Path.Combine(FolderName, mFileName);
}
}
private static string FolderName {
get {
return Path.Combine(Application.persistentDataPath, mFolderName);
}
}
[csharp] view plain copy
//初始化方法 如有需要,可重載初始化方法
public static void Init(string pFolderName, string pFileName) {
mFolderName = pFolderName;
mFileName = pFileName;
Dic_Value.Clear();
Read();
}
[csharp] view plain copy
//讀取文件及json數據載入到Dictionary中
private static void Read() {
if(!Directory.Exists(FolderName)) {
Directory.CreateDirectory(FolderName);
}
if(File.Exists(FileName)) {
FileStream fs = new FileStream(FileName, FileMode.Open);
StreamReader sr = new StreamReader(fs);
JsonData values = JsonMapper.ToObject(sr.ReadToEnd());
foreach(var key in values.Keys) {
Dic_Value.Add(key, values[key].ToString());
}
if(fs != null) {
fs.Close();
}
if(sr != null) {
sr.Close();
}
}
}
[csharp] view plain copy
//將Dictionary數據轉成json保存到本地文件
private static void Save() {
string values = JsonMapper.ToJson(Dic_Value);
Debug.Log(values);
if(!Directory.Exists(FolderName)) {
Directory.CreateDirectory(FolderName);
}
FileStream file = new FileStream(FileName, FileMode.Create);
byte[] bts = System.Text.Encoding.UTF8.GetBytes(values);
file.Write(bts,0,bts.Length);
if(file != null) {
file.Close();
}
}
到此,簡單的保存方法基本完成了。
三、舉例使用;
拿讀寫字元串為例:
[csharp] view plain copy
//判斷當前是否存在該key值
public static bool HasKey(string pKey) {
return Dic_Value.ContainsKey(pKey);
}
[csharp] view plain copy
//讀取string值
public static string GetString(string pKey) {
if(HasKey(pKey)) {
return Dic_Value[pKey];
} else {
return string.Empty;
}
}
[csharp] view plain copy
//保存string值
public static void SetString(string pKey, string pValue) {
if(HasKey(pKey)) {
Dic_Value[pKey] = pValue;
} else {
Dic_Value.Add(pKey, pValue);
}
Save();
}
php把生成的文件存放在指定目錄
PHP生成文件的時候,都可以執行到你希望存放的目錄,例如(UNIX):
$fp=fopen(“/tmp/abc.txt”,”W”);
再如(WINDOWS):
file_put_contents(‘c:/tmp/abc.txt’, ‘保存的內容’);
一般需要注意兩點,一是指定的目錄需要是存在的,如果目錄不存在會報錯,系統並不能夠自動建立目錄。二是對目錄要有許可權。
php 保存在指定的json文件?我是需要生成實體文件!
可以使用file_put_contents把字元串輸出到文件中.
file_put_contents(‘abc.josn’,$son_str);
原創文章,作者:OICY,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/140478.html