Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在数组键json中发布字符串?_Java_Json_Post - Fatal编程技术网

Java 如何在数组键json中发布字符串?

Java 如何在数组键json中发布字符串?,java,json,post,Java,Json,Post,我从json得到的url比我在json解码后得到的url要多,我得到的html比我需要发布到url的方式要多,因为有一个内容数组,html主体应该在其中发布 URL oracle = new URL(""); try (BufferedReader in = new BufferedReader( new InputStreamReader(oracle.openStream()))) { Stri

我从json得到的url比我在json解码后得到的url要多,我得到的html比我需要发布到url的方式要多,因为有一个内容数组,html主体应该在其中发布

 URL oracle = new URL("");
            try (BufferedReader in = new BufferedReader(
                    new InputStreamReader(oracle.openStream()))) {
                String inputLine1;
                while ((inputLine1 = in.readLine()) != null) {
                    System.out.println(inputLine1);
                    com.eclipsesource.json.JsonObject object = Json.parse(inputLine1).asObject();
                    com.eclipsesource.json.JsonArray items = Json.parse(inputLine1).asObject().get("data").asArray();

                    for (JsonValue item : items) {
                        //System.out.println(item.toString());
                        String name = item.asObject().getString("id", "Unknown Item");
                        System.out.println(name);

                        String quantity = item.asObject().getString("url", "id");
                       // JSONArray jsonArray2 = new JSONArray(quantity);
                         System.out.println(quantity);

                       /* Platform.runLater(() ->{
                            try {
                                Thread.sleep(10000);
                            } catch (InterruptedException ex) {
                                Logger.getLogger(HV1.class.getName()).log(Level.SEVERE, null, ex);
                            }*/
                        Img.load(quantity);
                                URL url;
        InputStream is = null;
        BufferedReader br;
        String line;
                 url = new URL(quantity);
            is = url.openStream();  // throws an IOException
            br = new BufferedReader(new InputStreamReader(is));

            while ((line = br.readLine()) != null) {
                System.out.println(line);
                //byte[] postData= line.getBytes( StandardCharsets.UTF_8 );
                    //String[] strArray = new String[] {line};
    //System.out.println(strArray[0]);
                wb2.load(line);
                String originalUrl = "";
    String newUrl = originalUrl.replace("ID", name);
    System.out.println(newUrl);

     URL url1 = new URL(newUrl);
            Map<String,Object> params = new LinkedHashMap<>();
            params.put("content", line);
            params.put("meta", "abc");


            StringBuilder postData1 = new StringBuilder();
            for (Map.Entry<String,Object> param : params.entrySet()) {
                if (postData1.length() != 0) postData1.append('&');
                postData1.append(URLEncoder.encode(param.getKey(), "UTF-8"));
                postData1.append('=');
                postData1.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));

            byte[] postDataBytes = postData1.toString().getBytes("UTF-8");

            HttpURLConnection conn = (HttpURLConnection)url1.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
            conn.setDoOutput(true);
            conn.getOutputStream().write(postDataBytes);

            Reader in1 = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

            for (int c; (c = in1.read()) >= 0;)
                System.out.print((char)c);

            /*          try{  
           Thread.sleep(400);  
          }catch(InterruptedException e){System.out.println(e);}  */
              }
                }
        } 
        }      
URL oracle=newurl(“”);
try(BufferedReader in=新的BufferedReader(
新的InputStreamReader(oracle.openStream())){
字符串输入1;
而((inputLine1=in.readLine())!=null){
System.out.println(inputLine1);
com.eclipsesource.json.JsonObject=json.parse(inputLine1.asObject();
com.eclipsesource.json.JsonArray items=json.parse(inputLine1.asObject().get(“data”).asArray();
用于(JsonValue项:项){
//System.out.println(item.toString());
字符串名称=item.asObject().getString(“id”,“未知项”);
System.out.println(名称);
字符串数量=item.asObject().getString(“url”、“id”);
//JSONArray jsonArray2=新JSONArray(数量);
系统输出打印项次(数量);
/*Platform.runLater(()->{
试一试{
睡眠(10000);
}捕获(中断异常例外){
Logger.getLogger(HV1.class.getName()).log(Level.SEVERE,null,ex);
}*/
Img.荷载(数量);
网址;
InputStream=null;
缓冲剂;
弦线;
url=新url(数量);
is=url.openStream();//引发IOException
br=新的BufferedReader(新的InputStreamReader(is));
而((line=br.readLine())!=null){
系统输出打印项次(行);
//byte[]postData=line.getBytes(StandardCharsets.UTF_8);
//字符串[]strArray=新字符串[]{line};
//System.out.println(strArray[0]);
wb2.荷载(线);
字符串originalUrl=“”;
字符串newUrl=originalUrl.replace(“ID”,名称);
System.out.println(newUrl);
URL url1=新URL(新URL);
Map params=新建LinkedHashMap();
参数put(“内容”,行);
参数put(“meta”、“abc”);
StringBuilder postData1=新的StringBuilder();
对于(Map.Entry参数:params.entrySet()){
如果(postData1.length()!=0)postData1.append('&');
append(URLEncoder.encode(param.getKey(),“UTF-8”);
postData1.append('=');
append(URLEncoder.encode(String.valueOf(param.getValue()),“UTF-8”);
字节[]postDataBytes=postData1.toString().getBytes(“UTF-8”);
HttpURLConnection conn=(HttpURLConnection)url1.openConnection();
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
conn.setRequestProperty(“内容长度”,String.valueOf(postDataBytes.Length));
连接设置输出(真);
conn.getOutputStream().write(postDataBytes);
Reader in1=新的BufferedReader(新的InputStreamReader(conn.getInputStream(),“UTF-8”);
for(int c;(c=in1.read())>=0;)
系统输出打印((字符)c);
/*试试{
睡眠(400);
}catch(InterruptedException e){System.out.println(e);}*/
}
}
} 
}      
这是代码,但在这个数据是在字符串中发布,而不是在数组中发布,因为我想在内容数组中发布html内容 这是url中的json,我必须在其中发布页面主体的content=html


正如你在输出中看到的,每次我必须在内容数组中一次发送完整的url时,点击url中的每一行html,即

,为了发送任何字符串(在你的html字符串中),你必须使用转义字符。对于JSON,转义字符是“/”

{
"html": "<div class=\"ad_box\"><img class=\"banner\" src=\"some_ad.png\" alt=\"\" \/> 
}
{
“html”:”
}

在上面的示例中,我在JSON键“html”中添加了示例html代码,并使用“\”作为转义字符来忽略双引号(“).

问题描述得非常糟糕。请尝试通过删减废话来更具体地解释。在url的内容数组中,我需要发布html Body无法理解问题描述。请添加示例输入和示例输出。Ganesh我用outpur和problem编辑过,但html在字符串行中,正如您在代码中看到的那样。您可以使用backticks``like:{“html”:
您的java字符串变量值在这里
}基本上我想在url中发布行变量数据