解析JSON数组

时间:2016-08-10 15:01:56

标签: java parsing simplejson

我正在尝试打印公司列表。

我的JSON对象:

public class ProductTypeParser {

    public  void parseJson(JSONObject jsonObject) throws ParseException {



        JSONObject object = (JSONObject) jsonObject;
        String name = (String) object.get("Name");
        System.out.println(name);

        String age = (String) object.get("Author");
        System.out.println(age);

        //loop array        
        JSONArray msg = (JSONArray) object.get("Company List"); 
        Iterator<String> iterator = msg.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }       
    }
}

代码:

SEVERE: Servlet.service() for servlet [contextServlet] in context with path [/SpringSafeHouseService2.0] 
  threw exception [Request processing failed; nested exception is 
    java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.json.simple.JSONArray] 
  with root cause java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.json.simple.JSONArray

错误:

logging.level.net.myapp=ERROR
logging.level.net.myapp.package.MyClass=DEBUG
logging.level.org.hibernate=ERROR
logging.level.org.springframework.scheduling=ERROR
logging.level.root=ERROR

4 个答案:

答案 0 :(得分:2)

转换为ArrayList而不是JSONArray(因为它可以是JSONArray或ArrayList或List的实现,它取决于你如何反序列化字符串,所以使用ArrayList或接口List确保你不能有类转换异常):

JSONObject parser = new JSONParser();
    JSONObject object = (JSONObject) parser.parse("{\n" +
            "    \"Name\": \"crunchify.com\",\n" +
            "    \"Author\": \"App Shah\",\n" +
            "    \"Company List\": [\n" +
            "        \"Compnay: eBay\",\n" +
            "        \"Compnay: Paypal\",\n" +
            "        \"Compnay: Google\"\n" +
            "    ]\n" +
            "}"); // Here there is a JSONArray for companies property

    Map<String, Object> obj = new HashMap();
    List<String> companies = new ArrayList<>();
    companies.add("a");
    companies.add("b");
    companies.add("c");
    obj.put("Name", "toto");
    obj.put("Author", "titi");
    obj.put("Company List", companies);
    JSONObject object2 = new JSONObject(obj); // here you have an ArrayList for companies property

    String name = (String) object.get("Name");
    System.out.println(name);

    String age = (String) object.get("Author");
    System.out.println(age);

    //loop array
    List msg = (List) object.get("Company List");
    Iterator<String> iterator = msg.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }

答案 1 :(得分:0)

尝试使用JSONArray msg = object.getJSONArray("Company List");代替JSONArray msg = (JSONArray) object.get("Company List");

答案 2 :(得分:0)

您正在使用具有不同界面的org.json.simple

以下是迭代ArrayList

的方法
 for(String s : object.get("Company List"))
      System.out.println(s);    

答案 3 :(得分:0)

Json-simple(您似乎正在使用虽然版本丢失,因此我的信息可能已过时)通常会为json中的数组创建JSONArray实例(扩展ArrayList btw。)字符串,即您的"Company List"字段。

但是,您也可以传入不同的ContainerFactory,这可能会为数组创建不同的List实例,这可能是您的演员无法工作的原因(同样在那里&# 39;你的问题中的信息太少,无法对此进行进一步评论。

最后,既然你应该得到一个List,那么应该安全地投射到那个:

List<?> msg = (List<?>) object.get("Company List");