从文件解析json

#json.txt

 {

name:"中国",
provinces:[
{name:"黑龙江",
citys:["佳木斯","大庆","哈尔滨","齐齐哈尔","牡丹江"]
},
{name:"辽宁",
citys:["沈阳","大连","盘锦"]
},
{name:"吉林",
citys:["吉林市","长春市","四平市"]
}
]
}
#JSonParser .java
import java.io.*;
import java.text.ParseException;
 
import org.json.*;
 
/**
 * 从json文件读取数据,形成JSon对象,读取对象数据并打印
 */
public class JSonParser {
 
public static void main(String[] args) {
//从JSon文件读取数据
StringBuffer stringBuffer = new StringBuffer();
String line = null ;
try {
BufferedReader br = new BufferedReader(new FileReader(new File("json.txt")));
while( (line = br.readLine())!= null ){
stringBuffer.append(line);
} catch (FileNotFoundException e) {
       e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
//将Json文件数据形成JSONObject对象
JSONObject jsonObject = new JSONObject(stringBuffer.toString());
//获取JSONObject对象数据并打印
JSONArray provinces = jsonObject.getJSONArray("provinces") ;
String name = null ;
StringBuffer jsonFileInfo = new StringBuffer();
JSONArray citys = null ;
for (int i = 0; i < provinces.length(); i++) {
name = provinces.getJSONObject(i).getString("name");
jsonFileInfo.append("/nname:" + name + "/n" + "citys:");
citys = provinces.getJSONObject(i).getJSONArray("citys");
for (int j = 0; j < citys.length(); j++) {
jsonFileInfo.append(citys.getString(j)+"/t");
}
}
 
System.out.println(jsonFileInfo);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
 
运行结果:
 
name:黑龙江
citys:佳木斯 大庆 哈尔滨 齐齐哈尔 牡丹江
name:辽宁
citys:沈阳 大连 盘锦
name:吉林
citys:吉林市 长春市 四平市
在Qt中,从文件解析JSON可以使用Qt自带的QFile和QJsonDocument类。下面是一个简单的示例代码: ```cpp #include <QFile> #include <QJsonDocument> #include <QJsonObject> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QFile file("example.json"); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "Failed to open file."; return -1; } QByteArray jsonData = file.readAll(); QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData); if(!jsonDocument.isNull()) { if(jsonDocument.isObject()) { QJsonObject jsonObject = jsonDocument.object(); if(jsonObject.contains("name")) { QJsonValue nameValue = jsonObject.value("name"); if(nameValue.isString()) { QString name = nameValue.toString(); qDebug() << "name:" << name; } } if(jsonObject.contains("age")) { QJsonValue ageValue = jsonObject.value("age"); if(ageValue.isDouble()) { int age = ageValue.toInt(); qDebug() << "age:" << age; } } if(jsonObject.contains("city")) { QJsonValue cityValue = jsonObject.value("city"); if(cityValue.isString()) { QString city = cityValue.toString(); qDebug() << "city:" << city; } } } } file.close(); return a.exec(); } ``` 代码中,首先定义了一个文件名为example.json文件,然后通过QFile::open()方法打开文件并读取文件中的JSON数据。接着,通过QJsonDocument::fromJson()方法将JSON数据转换为QJsonDocument对象。判断QJsonDocument对象是否为空和是否为对象类型,如果是,就通过QJsonObject类获取JSON对象中的属性值,判断属性值的类型后,获取其值并输出。最后,关闭文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值