.keys()方法不适用于JSONObject eclipse

时间:2017-08-28 09:12:01

标签: java json

我正在尝试使用.keys()方法来获取JSON Object的名称 使用的代码是;

Iterator<String> keys = JSONObject.keys();

.keys()在日食时没有变成红色,我不知道为什么,任何人都可以帮忙,谢谢! -

enter image description here

我将JSON简单地作为外部库并导入它,不知道还有什么要做

编辑:

这是更多的代码;

    JSONParser parser = new JSONParser();
            FileReader testfile = new FileReader("test2.txt");
            Object obj = parser.parse(testfile); 
            JSONObject jsonObject = (JSONObject) obj;
            JSONObject name = (JSONObject) jsonObject.get("txt");
            String time = (String) name.get("name");
            JSONObject example2 = (JSONObject) jsonObject.get("birth");
            System.out.println(example2);



        Iterator keys = example2 .keys(); <-- where the red line shows up

第二次编辑: 这是我的进口。

 import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
     import org.json.simple.parser.ParseException;

2 个答案:

答案 0 :(得分:1)

尝试:

  Iterator<String> keys = example2.keySet().iterator();

答案 1 :(得分:0)

使用此工件:https://mvnrepository.com/artifact/org.json/json/20170516

使用以下代码:

JSONObject obj = new JSONObject("{\"key\":\"value\"}");
for(Object o : obj.keys()) { ... }

它会起作用。

不要混用API。