代码之家  ›  专栏  ›  技术社区  ›  user4213837

如何从JSONArray解析JSON对象?

  •  1
  • user4213837  · 技术社区  · 7 年前

    [{"fullname": "name1", "id": "123"}, {"fullname": "name2", "id": "245"}, {"fullname": "name3", "id": "256"}]
    

    它看起来像一个JSONArray。所有记录都写在同一行中。

    你能帮我怎么用Java解析这个文件吗。我想读取每个JSON对象并显示所有全名和ID。以下是我的尝试,但我的代码不起作用:

    import org.apache.commons.lang3.StringEscapeUtils;
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
    
    public class JSONFileReaderDriver {
    
    public static void main(String[] args) throws FileNotFoundException, 
    IOException, ParseException 
    {
     String filename="Aarau";
     String src="C:\\"+filename+".json";
     JSONParser parser = new JSONParser();
     JSONObject obj;
     try
        {
            BufferedReader br=new BufferedReader (new FileReader(src));  
            obj = (JSONObject) new JSONParser().parse(row);
            String fullname=obj.get("fullname");
            String id=obj.get("id");
            System.out.println ("fullname: "+fullname+" id: "+id);
        }catch(Exception e)
         {e.printStackTrace();}
       br.close();
      }
     }
    
    5 回复  |  直到 7 年前
        1
  •  5
  •   Herr Derb    7 年前

    让你的生活变得轻松,使用 ObjectMapper . 这样,您只需定义一个与json对象具有相同属性的Pojo。

    在您的情况下,您需要一个如下所示的Pojo:

    public class Person{
        private String fullname;
        private int id;
    
        public Person(String fullname, int id) {
            this.fullname = fullname;
            this.id = id;
        }
    
        public String getFullname() {
            return fullname;
        }
    
        public int getId() {
            return id;
        }
    }
    

    这样,您只需执行以下操作:

    ObjectMapper objectMapper = new ObjectMapper();
    List<Person> persons = objectMapper.readValue(myInputStream, TypeFactory.defaultInstance().constructCollectionType(List.class, Person.class));
    

    这是一种无麻烦且类型安全的方法。

    需要依赖项:

    https://github.com/FasterXML/jackson-databind

    <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
    </dependency>
    
        2
  •  4
  •   Rahul Rabhadiya    7 年前

    您可以使用Json。简单的java api,下面是对您有帮助的代码

            byte[] bFile = Files.readAllBytes(new File("C:/xyz.json").toPath());
            JSONArray root = (JSONArray) JSONValue.parseWithException(bFile);
            JSONObject rootObj = (JSONObject) root.get(0);
    

        3
  •  3
  •   user4213837    7 年前

    好的,各位。。。刚刚解决了我的问题。我张贴的解决方案,以防有人再次遇到相同的问题,可以使用我的解决方案。我的解决方案部分是由Rahul Rabhadiya提出的。谢谢,伙计。

    try{
     row=br.readLine();
                JSONArray root = (JSONArray) JSONValue.parseWithException(row);
    
                for (int i=0;i<root.size();i++)
                {
    
                JSONObject rootObj = (JSONObject) root.get(i);
                String fullname=(String) rootObj.get("fullname");
    
                System.out.println (fullname);
                }
        }catch(Exception e)
             {
            e.printStackTrace();
            }
    
        4
  •  3
  •   ajc    7 年前

    您的json是一个JSONArray,因此当您解析它时,您可以 需要将其解析为JSONArray .

            JSONParser jsonParser = new JSONParser();
            JSONArray a = (JSONArray) jsonParser.parse(new FileReader(src));
            for (Object o : a) {
                // access your object here. 
            }
    
        5
  •  0
  •   Gorazd Rebolj    7 年前

    我建议使用Jackson,尤其是Jackson流API,它非常适合解析这样的大型数组。

    请参见以下答案: https://stackoverflow.com/a/24838392/3765428