当前位置:首页>开发>正文

如何读取一个本地Json文件并查询该文件展示其内容 如何读取本地的json数据

2023-05-18 08:56:43 互联网 未知 开发

 如何读取一个本地Json文件并查询该文件展示其内容 如何读取本地的json数据

如何读取一个本地Json文件并查询该文件展示其内容

String fullFileName = "E:/a.json"

File file = new File(fullFileName)
Scanner s 如何读取一个本地Json文件并查询该文件展示其内容 如何读取本地的json数据canner = null
StringBuilder buffer = new StringBuilder()
try {
scanner = new Scanner(file, "utf-8")
while (scanner.hasNextLine()) {
buffer.append(scanner.nextLine())
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block

} finally {
if (scanner != null) {
scanner.close()
}
}

System.out.println(buffer.toString())

如何读取本地的json数据

本地有一个静态文件 index.html。一个index.js脚本 index.js中有 $scope.rows的JSON格式数据。

1 $scope.rows = [
2 { appId: 1, appName: "会计系统1" , appType: 1, description: "应用系统说明!" },
3 { appId: 2, appName: "会计系统2", appType: 2, description: "应用系统说明!" },
4 { appId: 3, appName: "会计系统3", appType: 3, description: "应用系统说明!" },
5 { appId: 4, appName: "会计系统4", appType: 3, description: "应用系统说明!" },
6 { appId: 5, appName: "会计系统5", appType: 2, description: "应用系统说明!" },
7 { appId: 6, appName: "会计系统6", appType: 1, description: "应用系统说明!" },
8 { appId: 7, appName: "会计系统7", appType: 2, description: "应用系统说明!" },
9 { appId: 8, appName: "会计系统8", appType: 1, description: "应用系统

微信小程序 如何解析Bmob返回的JSON数据

json数据格式解析我自己分为两种;
一种是普通的,一种是带有数组形式的;
普通形式的:
服务器端返回的json数据格式如下:

复制代码代码如下:

{"userbean":{"Uid":"100196","Showname":"u75afu72c2u7684u7334u5b50","Avtar":null,"State":1}}

分析代码如下:

复制代码代码如下:

// TODO 状态处理 500 200
int res = 0
res = httpClient.execute(httpPost).getStatusLine().getStatusCode()
if (res == 200) {
/*
* 当返回码为200时,做处理
* 得到服务器端返回json数据,并做处理

如何用angularjs读取本地json

很明显你是理解错了执行的先后顺序,你这样测试下:
$scope.callback=function(){
console.log($scope.phones)//输出undefined
console.log(test)//输出空Object
}

var test=new Object()
$http.get(phones/phones.json).success(function(data)
{
$scope.phones = data
test = data
console.log($scope.phones)//正常输出JSON对象
console.log(test)//正常输出JSON对象
$scope.callback()//换句话就是,ajax请求如果你没设定同步的话,请求后面定义的代码会先执行
})

如何从本地 读取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 Dictionary Dic_Value = new Dictionary()

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()
}

js怎么读取本地的 json数据


引用jQuery库使用ajax读取
 $.ajax({
                type: "GET",
                url: "Content/list.txt",//本地json文件路径
                dataType: "json",
                success: function (data) {
                    console.info(data)
                    for (var i = 0 i                         var trTD = "
""   data[i].optionKey   " "   data[i].optionValue   "
                        $("#tb").append(trTD)
                    }
                }
            })

如何读写json文件

publicstaticfinalpublicstaticfinalJSONArrayparseArray(Stringtext)//把 JSON 文本 parse 成 JSONArray
publicstaticfinalpublicstaticfinalStringtoJSONString(Objectobject)//将 JavaBean 序列化为 JSON 文本
publicstaticfinalStringtoJSONString(Objectobject,booleanprettyFormat)//将 JavaBean 序列化为带格式的 JSON 文本
publicstaticfinalObjecttoJSON(ObjectjavaObject)将 JavaBean 转换为 JSONObject 或者 JSONArray 。
在网上搜的api,你可以动手试试,总在fastJSON很好很强大!

最新文章