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

Java 使用JSONObject以URL格式传递Json数据的任何方法?,java,json,Java,Json,我创建了一个java URL类,其中包含我的Json数据,并具有一些函数来获取我的Json数据以进行数据比较,我发现JSONObject可能不支持将数据传递到JSONObject。因为我的JSON数据也有数组结构,所以我需要在我的例子中使用JSONArray吗 try { JSONObject obj = new JSONObject (); obj.readJsonFromUrl(theUrl);

我创建了一个java URL类,其中包含我的Json数据,并具有一些函数来获取我的Json数据以进行数据比较,我发现JSONObject可能不支持将数据传递到JSONObject。因为我的JSON数据也有数组结构,所以我需要在我的例子中使用JSONArray吗

     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方法
私有静态JSONObject readJsonFromUrl(URL theUrl)抛出IOException,JSONException{


任何人都可以向我解释JSON数据在java中是如何工作的?我看到了很多JSON的java类,这让我感到困惑,比如JSONObject、JSONArray、JSONValue。我在网上搜索了一些信息,但我也不是很清楚,因为我对JSON数据处理非常陌生

这是我的JSON数据示例和数据我只需要扫描结果

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method readJsonFromUrl(URL) is undefined for the type JSONObject
{  
   "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":{  

   }
} 

您不能在url中传递json,您可以在正文中传递。将json写入并使用正则表达式发布。 这是对你问题的解释

所需的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("-----------");
            }
        }

您不能在url中传递json,您可以在正文中传递。将json写入并使用正则表达式发布。 这是对你问题的解释

所需的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("-----------");
            }
        }
如前所述,有很多方法可以解决这个问题。要么你自己实现读取、解析操作(@Roland Illig的答案)

或者你也可以使用图书馆。最著名和最广泛使用的图书馆是和。 重要的是,您尝试将json对象“映射”到一个类

您有您的json文件:

{
“id”:1,
“姓名”:“艾里尼”,
“爱好”:[“音乐”、“哲学”、“足球”]
}

以及一个表示此文件并将存储值的类(根据您使用的库可能有不同的要求,例如getter、setter等)

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

或者你也可以使用图书馆。最著名和最广泛使用的图书馆是和。 重要的是,您尝试将json对象“映射”到一个类

您有您的json文件:

{
“id”:1,
“姓名”:“艾里尼”,
“爱好”:[“音乐”、“哲学”、“足球”]
}

以及一个表示此文件并将存储值的类(根据您使用的库可能有不同的要求,例如getter、setter等)


你确定readJsonFromUrl方法在JSONOBject类中有吗?@SpringLearner-我尝试更改为getJsonObject,它会显示相同的错误消息。你能推荐JSONOBject中的任何合适方法吗?你是指JavaEE7中的
javax.json.JSONOBject
?@HeartBreakKID向我们展示示例json吗?之后我们可以告诉你确切的情况way@isuru-buddhika-我指的是org.json.JSONObject;你确定在JSONObject类中有readJsonFromUrl方法吗?@SpringLearner-我尝试更改为getJsonObject,并显示相同的错误消息。你能推荐JSONObject中任何合适的方法吗?你指的是
javax.json.JSONObject吗ct
在JavaEE7中?@HeartBreakKID向我们展示JSON样本?然后我们可以告诉您确切的结果way@isuru-buddhika——我指的是org.json.JSONObject;
  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());