关于phpjson文件保存本地的信息

本文目录一览:

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/n/140478.html

(0)
OICYOICY
上一篇 2024-10-04
下一篇 2024-10-04

相关推荐

发表回复

登录后才能评论