通过使用JAVA提供特定id-JSON文件来获取特定值
我想读取并打印特定通过使用JAVA提供特定id-JSON文件来获取特定值,java,json,Java,Json,我想读取并打印特定id的值。例如,我想用id=1读取并打印传感器的名称和状态。 如何使用JAVA和JSON文件实现这一点?有人能帮我吗 { "Sensor": [ { "id": 1, "name": "RR", "status": 1, }, { "id": 2, "name": "RS", "
id
的值。例如,我想用id=1
读取并打印传感器的名称和状态。
如何使用JAVA
和JSON
文件实现这一点?有人能帮我吗
{
"Sensor": [
{
"id": 1,
"name": "RR",
"status": 1,
},
{
"id": 2,
"name": "RS",
"status": 1,
},
{
"id": 3,
"name": "GR",
"status": 0,
},
],
}
要读取JSON
文件的JAVA代码:
public class JSON {
private static String jsonFile = "/Users/foteini/Desktop/JSON/sensor copy.json";
public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
FileReader reader = new FileReader(jsonFile);
JSONObject jsonObject = (JSONObject) new JSONParser().parse(reader);
JSONArray sensors = (JSONArray) jsonObject.get("Sensor");
//take the elements of the json array
int id_num=0;
for (int i=0; i<sensors.size(); i++){
System.out.println("The sensors in the array:" + sensors.get(i) + "\n");
}
}
}
公共类JSON{
私有静态字符串jsonFile=“/Users/foteini/Desktop/JSON/sensor copy.JSON”;
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException、ParseException{
FileReader=新的FileReader(jsonFile);
JSONObject JSONObject=(JSONObject)新建JSONParser().parse(读取器);
JSONArray传感器=(JSONArray)jsonObject.get(“传感器”);
//以json数组的元素为例
int id_num=0;
对于(inti=0;ipublic类JSON){
/**
*@param指定命令行参数
*/
私有静态字符串jsonFile=“/Users/foteini/Desktop/JSON/sensor copy.JSON”;
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException、ParseException{
FileReader=新的FileReader(jsonFile);
JSONObject JSONObject=(JSONObject)新建JSONParser().parse(读取器);
JSONArray传感器=(JSONArray)jsonObject.get(“传感器”);
//以json数组的元素为例
int id_num=0;
对于(int i=0;i您的传感器是JSONArray
而不是JSONObject
so
1.)获取您的阵列
2.)使用索引遍历数组并从数组中提取对象
3.)从对象获取值
JSONObject jsonObject = (JSONObject) new JSONParser().parse(reader);
// fetch sensor array
JSONArray sensors = jsonObject.getJSONArray("Sensor");
JSONObject temp;
//take the elements of the json array
int id ;
String name,status;
for (int i=0; i<sensors.size(); i++){
// fetch array using index
temp = sensors.getJSONObject(i);
// fetch your data
id = temp.optInt("id");
if( id==1){
name = temp.optString("name");
status = temp.optString("status");
System.out.println( name +" "+status);
}
}
JSONObject JSONObject=(JSONObject)新建JSONParser().parse(reader);
//提取传感器阵列
JSONArray sensors=jsonObject.getJSONArray(“传感器”);
JSONObject温度;
//以json数组的元素为例
int-id;
字符串名称、状态;
对于(int i=0;我可能重复感谢您的回复!但我收到一些错误,如“找不到符号符号:方法getJSONArray(字符串)位置:类型为jsonObject的变量jsonObject”,“找不到符号符号符号:方法getJSONObject(int)位置:类型为JSONArray的变量传感器”,optInt、optString出现类似错误。我使用的是import org.json.simple。你知道我为什么会出现这些错误吗?@foteinik你必须下载并附加json.org
库作为项目的依赖项谢谢你的回复!!我尝试了你的答案,但在getInt和getString上出现了两个错误。错误是“找不到symbol symbol:method getInt(String)location:variable item of type JSONObject”,getString也有类似的错误。你知道为什么会出现这些错误吗?我使用的是import org.json.simple。@在本例中,Foteinick用显式强制转换替换这些方法。(Integer)item.get(“id”)
和(String)item.get(“status”)
。下面是一个很好的例子:正如您所说,我替换了它,但现在当我运行代码时,只打印出第一条传感器记录,然后我接受一个错误/异常。输出看起来像“数组中的传感器:{“id”:0,“status”:1,“link”:“\/s00\/”,“name”:“RR”}线程“main”中的异常java.lang.ClassCastException:java.lang.Long不能在json.json.main的json.json.main(json.java:49)处转换为java.lang.Integer,java结果:1”。
JSONObject jsonObject = (JSONObject) new JSONParser().parse(reader);
// fetch sensor array
JSONArray sensors = jsonObject.getJSONArray("Sensor");
JSONObject temp;
//take the elements of the json array
int id ;
String name,status;
for (int i=0; i<sensors.size(); i++){
// fetch array using index
temp = sensors.getJSONObject(i);
// fetch your data
id = temp.optInt("id");
if( id==1){
name = temp.optString("name");
status = temp.optString("status");
System.out.println( name +" "+status);
}
}