使用JSONObject以URL格式传递Json数据的任何方法?

时间:2016-11-19 07:34:24

标签: java json

我创建了一个包含我的Json数据的java URL类,并且有一些函数可以获取我的json数据以进行一些数据比较,我发现它可能不支持JSONObject将数据传递给JSONObject。在我的情况下是否需要使用JSONArray,因为我的JSON数据也有数组结构?

     try
           {
            JSONObject obj = new JSONObject (); 
            obj.readJsonFromUrl(theUrl);
            System.out.println(obj.toString());
           }

       catch(MalformedURLException e)
       {
           System.out.print("your problem here ...1");
       }
   }
   else
   {
       System.out.print("Can't Connect");
   }


我确定这是给我错误信息的地方因为它在我的编译器中将此错误返回给我

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method readJsonFromUrl(URL) is undefined for the type JSONObject



对于JSONObject readJsonFromUrl方法,还有一些警告信息     private static JSONObject readJsonFromUrl(URL theUrl) throws IOException, JSONException {

任何人都可以向我提供JSON数据如何在java中工作的解释?我看到了很多JSON的Java类,让我对它感到困惑,比如JSONObject,JSONArray,JSONValue。我在网上搜索一些信息,但我也不太清楚,因为我对JSON数据处理很新。
这是我的样本json数据,我需要的数据只是scan_result

{  
   "data_id":"a71a3c2588c6472bb4daea41a0b58835",
   "file_info":{  
      "display_name":"",
      "file_size":242,
      "file_type":"Not available",
      "file_type_description":"Not available",
      "md5":"aa69ba384f22d0dc0551ace2fbb9ad55",
      "sha1":"09ceb54e65df3d3086b222e8643acffe451a6e8a",
      "sha256":"dcb46d6ae2a187f789c12f19c44bbe4b9a43bd200a3b306d5e9c1fcf811dc430",
      "upload_timestamp":"2016-11-18T09:09:08.390Z"
   },
   "process_info":{  
      "blocked_reason":"",
      "file_type_skipped_scan":false,
      "post_processing":{  
         "actions_failed":"",
         "actions_ran":"",
         "converted_destination":"",
         "converted_to":"",
         "copy_move_destination":""
      },
      "profile":"File scan",
      "progress_percentage":100,
      "result":"Allowed",
      "user_agent":""
   },
   "scan_results":{  
      "data_id":"a71a3c2588c6472bb4daea41a0b58835",
      "progress_percentage":100,
      "scan_all_result_a":"No Threat Detected",
      "scan_all_result_i":0,
      "scan_details":{  
         "Ahnlab":{  
            "def_time":"2016-11-08T15:00:00.000Z",
            "location":"local",
            "scan_result_i":0,
            "scan_time":1,
            "threat_found":""
         },
         "Avira":{  
            "def_time":"2016-11-08T00:00:00.000Z",
            "location":"local",
            "scan_result_i":0,
            "scan_time":133,
            "threat_found":""
         },
         "ClamAV":{  
            "def_time":"2016-11-08T10:28:00.000Z",
            "location":"local",
            "scan_result_i":0,
            "scan_time":94,
            "threat_found":""
         },
         "ESET":{  
            "def_time":"2016-11-08T00:00:00.000Z",
            "location":"local",
            "scan_result_i":0,
            "scan_time":38,
            "threat_found":""
         }
      },
      "start_time":"2016-11-18T09:09:08.405Z",
      "total_avs":4,
      "total_time":250
   },
   "vulnerability_info":{  

   }
} 

2 个答案:

答案 0 :(得分:0)

你不能在网址中传递json,你可以在体内传递它。将Json写入stream body并使用常规java method发布。 这是oracle community url解释你的问题。

可以从here下载所需的Jar。

测试代码遵循:

   URL url = new URL("https://graph.facebook.com/search?q=java&type=post");
         try (InputStream is = url.openStream();
              JsonReader rdr = Json.createReader(is)) {

             JsonObject obj = rdr.readObject();
             JsonArray results = obj.getJsonArray("data");
             for (JsonObject result : results.getValuesAs(JsonObject.class)){
                 System.out.print(result.getJsonObject("from").getString("name"));
                 System.out.print(": ");
                System.out.println(result.getString("message", ""));
               System.out.println("-----------");
            }
        }

答案 1 :(得分:0)

如上所述here,有很多方法可以解决这个问题。要么你必须自己实现读取,解析操作(@Roland Illig的回答)

//you have to implement the readJSON method 
InputStream is = new URL(url).openStream();
try {
  BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
  String jsonText = readAll(rd);
  JSONObject json = new JSONObject(jsonText);
  return json;
} finally {
  is.close();
}

或者您可以使用图书馆。最着名和最广泛使用的库是jacksongson。 大局是你试图"映射"你的json对象是一个类。

你有你的json文件:

{ "id":1, "name":"eirini", "hobbies":["music","philosophy","football"] }

和一个代表此文件并将存储值的类(取决于您使用的库可能有不同的要求,例如getter,setter等。)

public class Person {
   public int id;
   public String name;
   public List<String> hobbies = new ArrayList<String>();

   public String toString() {
       return name +" has the id: " + id + " the following hobbies" + hobbies.get(0) + " " + hobbies.get(2); 
   }
}

最后在你的主要方法中:

  public static void main(String[] args) throws IOException, ParseException {
    ObjectMapper mapper = new ObjectMapper();
    InputStream input = this.getClass().getResourceAsStream(FILE); //read your file. There are many ways to achieve this.
    ObjectMapper mapper = new ObjectMapper(); // just need one
    Person eirini = mapper.readValue(input, Person.class);
    System.out.println(eirini.toString());