如何从文件中解析这个 json
How to parse this json from file
如何使用 SimpleJson 库解析此 json 文件,格式如下:
谢谢
我的文件如下所示:json 文件,其中包含 Json 数组;
{"data":[{"host":"hostname1","port":2049,"open":"false", "info":" "},
{"host":"hostname1","port":2049,"open":"false", "info":" "},
{"host":"hostname2","port":2049,"open":"false", "info":" "},
{"host":"hostname3","port":2049,"open":"false", "info":" "},
{"host":"hostname4","port":443,"open":"false", "info":" "},
{"host":"hostname5","port":443,"open":"false","info":" "},
{"host":"hostname6","port":61208,"open":"false","info":" "},
{"host":"hostname7","port":139,"open":"false","info":" "}]}
此时我的代码:
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("D:/file.json"));
JSONArray jsonObject = (JSONArray) obj;
JSONObject arr = (JSONObject) jsonObject.get(0);
JSONArray arguments = (JSONArray) arr.get("arguments");
System.out.println("arguments>>>>>>>>> "+arguments);
for(int i = 0 ; i< arguments.size() ;i++){
JSONObject object = (JSONObject) arguments.get(i);
System.out.println(object);
return object;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
我是这样手动操作的:
//data
JSONObject obj1 = new JSONObject();
obj1.put("host", "mkyong.com");
obj1.put("port", "555");
obj1.put("open", "false");
obj1.put("info", "");
JSONObject obj2 = new JSONObject();
obj2.put("host", "mkyong.com");
obj2.put("port", "555");
obj2.put("open", "false");
obj2.put("info", "");
JSONArray list = new JSONArray();
list.add(obj2);
JSONObject datajson = new JSONObject();
datajson.put("data", list);
看起来 JSON.simple
是一个旧的 Google 库。我认为您需要切换到 GSON
。这是演示如何读取给定 JSON
的示例代码。这里的Key是原来的JSON
对象持有一个JSON
对象的数组作为keydata
.
的值
import java.io.FileReader;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
/**
*
* @author blj0011
*/
public class JsonSimpleReaderExample
{
public static void main(String[] args)
{
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("file.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray array = (JSONArray) jsonObject.get("data");
Iterator<JSONObject> iterator = array.iterator();
while (iterator.hasNext()) {
JSONObject jsonObjectInJsonArray = (JSONObject) iterator.next();
System.out.println(jsonObjectInJsonArray.get("host"));
}
}
catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
输出:
hostname1
hostname1
hostname2
hostname3
hostname4
hostname5
hostname6
hostname7
如何使用 SimpleJson 库解析此 json 文件,格式如下:
谢谢
我的文件如下所示:json 文件,其中包含 Json 数组;
{"data":[{"host":"hostname1","port":2049,"open":"false", "info":" "},
{"host":"hostname1","port":2049,"open":"false", "info":" "},
{"host":"hostname2","port":2049,"open":"false", "info":" "},
{"host":"hostname3","port":2049,"open":"false", "info":" "},
{"host":"hostname4","port":443,"open":"false", "info":" "},
{"host":"hostname5","port":443,"open":"false","info":" "},
{"host":"hostname6","port":61208,"open":"false","info":" "},
{"host":"hostname7","port":139,"open":"false","info":" "}]}
此时我的代码:
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("D:/file.json"));
JSONArray jsonObject = (JSONArray) obj;
JSONObject arr = (JSONObject) jsonObject.get(0);
JSONArray arguments = (JSONArray) arr.get("arguments");
System.out.println("arguments>>>>>>>>> "+arguments);
for(int i = 0 ; i< arguments.size() ;i++){
JSONObject object = (JSONObject) arguments.get(i);
System.out.println(object);
return object;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
我是这样手动操作的:
//data
JSONObject obj1 = new JSONObject();
obj1.put("host", "mkyong.com");
obj1.put("port", "555");
obj1.put("open", "false");
obj1.put("info", "");
JSONObject obj2 = new JSONObject();
obj2.put("host", "mkyong.com");
obj2.put("port", "555");
obj2.put("open", "false");
obj2.put("info", "");
JSONArray list = new JSONArray();
list.add(obj2);
JSONObject datajson = new JSONObject();
datajson.put("data", list);
看起来 JSON.simple
是一个旧的 Google 库。我认为您需要切换到 GSON
。这是演示如何读取给定 JSON
的示例代码。这里的Key是原来的JSON
对象持有一个JSON
对象的数组作为keydata
.
import java.io.FileReader;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
/**
*
* @author blj0011
*/
public class JsonSimpleReaderExample
{
public static void main(String[] args)
{
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("file.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray array = (JSONArray) jsonObject.get("data");
Iterator<JSONObject> iterator = array.iterator();
while (iterator.hasNext()) {
JSONObject jsonObjectInJsonArray = (JSONObject) iterator.next();
System.out.println(jsonObjectInJsonArray.get("host"));
}
}
catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
输出:
hostname1
hostname1
hostname2
hostname3
hostname4
hostname5
hostname6
hostname7