[英]Read JSON file in Java
我有一個包含鍵-值類型信息的JSON文件。 此信息由存儲在8x8矩陣中的字符組成。 當我閱讀它返回文件的全部內容時,我想做的就是獲取矩陣每一行的值。
{"1":"1 1 1 1 1 1 1 1","2":"1 1 O O O O 1 1","3":"1 1 O O O O 1 1","4":"O 1
1 O O 1 1 O","5":"O 1 1 1 1 1 1 O","6":"O O 1 1 1 1 O O","7":"O O O O O O
O O","8":"O O O O O O O O","time":"0.2"}
這是文件的內容,向我顯示了控制台,我想向我顯示的只是1和O。
public static void main(String[] args) throws InterruptedException {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("pruebame.json"));
JSONObject jsonObject = (JSONObject) obj;
// loop array
JSONArray tag = (JSONArray) jsonObject.get("Tags");
Iterator iterator = tag.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
我需要得到以下格式:
1 1 1 1 1 1 1 1
1 1 O O O O 1 1
1 1 O O O O 1 1
O 1 1 O O 1 1 O
O O 1 1 1 1 O O
O O 1 1 1 1 O O
謝謝!
使用jsonObject.getString("idName")
for(int i=1; i<=8; i++){
System.out.println(jsonObject.getString("" + i));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.