解析特定案例的JSON

时间:2016-07-13 09:05:17

标签: java json

我正在尝试从imagga的图像识别API中检索以下JSON数据。

{"results":[{"image":"http://docs.imagga.com/static/images/docs/sample/japan-605234_1280.jpg","tagging_id":null,"tags":[{"confidence":63.346307851163395,"tag":"valley"},{"confidence":60.66263009377379,"tag":"mountain"},{"confidence":44.39096006516168,"tag":"canyon"},{"confidence":42.08210930346856,"tag":"landscape"},{"confidence":33.52198895357515,"tag":"geological formation"},{"confidence":32.702112467737216,"tag":"mountains"},{"confidence":28.626223994488203,"tag":"glacier"},{"confidence":28.36,"tag":"natural depression"},{"confidence":28.03481906795487,"tag":"ravine"},{"confidence":27.269738461024804,"tag":"sky"},{"confidence":26.130797131953397,"tag":"rock"},{"confidence":23.11898739400327,"tag":"travel"},{"confidence":21.75182989551758,"tag":"alp"},{"confidence":20.956625061326214,"tag":"national"},{"confidence":20.15360199670358,"tag":"park"},{"confidence":19.826365024393702,"tag":"stone"},{"confidence":19.717420656127437,"tag":"water"},{"confidence":18.049071926896588,"tag":"river"},{"confidence":17.81629840041474,"tag":"hill"},{"confidence":17.30594970410163,"tag":"tourism"},{"confidence":17.192663177192692,"tag":"clouds"},{"confidence":16.53588724897844,"tag":"scenic"},{"confidence":15.98967256769248,"tag":"peak"},{"confidence":15.792599629554461,"tag":"lake"},{"confidence":15.532788988165363,"tag":"scenery"},{"confidence":15.453814687301834,"tag":"snow"},{"confidence":15.232632664896412,"tag":"outdoors"},{"confidence":15.212304004139495,"tag":"range"},{"confidence":15.042325772263556,"tag":"hiking"},{"confidence":14.958759294889424,"tag":"tree"},{"confidence":14.78842712696222,"tag":"forest"},{"confidence":12.853490785491731,"tag":"grass"},{"confidence":12.242518977753525,"tag":"desert"},{"confidence":12.095999999999998,"tag":"natural elevation"},{"confidence":12.03899501602295,"tag":"america"},{"confidence":11.49381779097963,"tag":"environment"},{"confidence":11.250534926394025,"tag":"usa"},{"confidence":10.935999552280517,"tag":"panorama"},{"confidence":10.838870815021957,"tag":"trees"},{"confidence":10.77081532273937,"tag":"south"},{"confidence":10.385222667460749,"tag":"summer"},{"confidence":9.967993711501377,"tag":"cloud"},{"confidence":9.960797892906747,"tag":"wild"},{"confidence":9.840206836878211,"tag":"natural"},{"confidence":9.64736797817423,"tag":"geology"},{"confidence":9.622992778171428,"tag":"rocky"},{"confidence":9.5011692563878,"tag":"outdoor"},{"confidence":9.36921935993258,"tag":"wilderness"},{"confidence":9.360136841263397,"tag":"vacation"},{"confidence":9.295849004816608,"tag":"rocks"},{"confidence":9.200756690906687,"tag":"high"},{"confidence":9.098263071652019,"tag":"highland"},{"confidence":8.912795414022,"tag":"tourist"},{"confidence":8.871604649828521,"tag":"hike"},{"confidence":8.849249986309006,"tag":"landmark"},{"confidence":8.696713373486205,"tag":"cliff"},{"confidence":8.600291951670297,"tag":"scene"},{"confidence":8.535889495009538,"tag":"stream"},{"confidence":8.530021520404471,"tag":"sunny"},{"confidence":8.255077489679804,"tag":"altitude"},{"confidence":8.016191292928964,"tag":"trail"},{"confidence":7.9938748285500605,"tag":"autumn"},{"confidence":7.985278417869093,"tag":"california"},{"confidence":7.927492176055299,"tag":"spain"},{"confidence":7.774043777890904,"tag":"adventure"},{"confidence":7.560207874392119,"tag":"peaceful"},{"confidence":7.485827508554503,"tag":"fall"},{"confidence":7.283862421876644,"tag":"erosion"},{"confidence":7.272123549182718,"tag":"terrain"},{"confidence":7.24510515635207,"tag":"rural"},{"confidence":7.234934522337296,"tag":"vista"},{"confidence":7.092282542389207,"tag":"holiday"}]}]}

我正在使用http://www.java2s.com/Code/Jar/o/Downloadorgjson20130603jar.htm库。

我的Java代码如下:

String imageUrl = "http://docs.imagga.com/static/images/docs/sample/japan-605234_1280.jpg",
                            apiKey = "",
                            apiSecret = "";

                            // These code snippets use an open-source library. http://unirest.io/java

                            HttpResponse response = Unirest.get("https://api.imagga.com/v1/tagging")
                                    .queryString("url", imageUrl)
                                    .basicAuth(apiKey, apiSecret)
                                    .header("Accept", "application/json")
                                    .asJson();


            String js = response.getBody().toString();
                        System.out.println(js.toString());


                        JSONObject jObject = new JSONObject(response.getBody()); // json
                        System.out.print("hello");
                        JSONObject data1 = jObject.getJSONObject("results"); // get data
                        System.out.print(data1);                                            // object
                        String projectname = data1.getString("tags"); // get the name
                                                                        // from data.
                        System.out.print(projectname);

我收到错误

  

线程“main”中的异常org.json.JSONException:   找不到JSONObject [“results”]。

我想要的是“标签”和“信心”列表。

4 个答案:

答案 0 :(得分:0)

试试这个

   JSONArray data1 = jObject.getJSONArray("results");

已编辑的答案

    String js = response.getBody().toString();
    System.out.println(js.toString());


    JSONObject jObject = new JSONObject(js); // json
    System.out.print("hello");
    JSONArray data1 = jObject.getJSONArray("results");

    for(int i = 0; i < data1.length; i++)
    {
        JSONObject jsonObject = data1.getJSONObject(i);
        String projectn ame = jsonObject.getString("tagging_id");                                                                       
        System.out.print(projectname);

        JSONArray tagArray = jsonObject.getJsonArray("tags");

        for(int j = 0; j < tagArray.length; j++)
        {
             JSONObject tagObject = tagArray.getJSON(j);
             System.out.println("Tag == " + tagObject.getString("tag"));
        }

    }

答案 1 :(得分:0)

我想你应该尝试jObject.getJSONArray("results")而不是jObject.getJSONObject("results")

还有一个很好的工具可以将json转换为java:http://json2java.azurewebsites.net/

如果您使用它,您将看到:

    ...other code...

    public class RootObject
    {
      private ArrayList<Result> results;

      public ArrayList<Result> getResults() { return this.results; }

      public void setResults(ArrayList<Result> results) { this.results = results; }
    }

...other code...

此处列出了results,因此请使用getJSONArray代替getJSONObject

答案 2 :(得分:0)

为了让你的生活更轻松,我会把对象模型化为POJO,让杰克逊的Objectmapper发挥魔力。

请参阅http://wiki.fasterxml.com/JacksonDataBinding

答案 3 :(得分:0)

如果你查看你的json results中有一个数组,那么你应该使用getJSONArray而不是getJSONObject

JSONArray data1 = jObject.getJSONArray("results");