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

时间:2017-12-20 09:17:31

标签: java json post

我从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);}  */
              }
                }
        } 
        }      

这是代码,但是这个数据是在字符串中发布而不是在数组中,因为我想在内容数组中发布html内容 这是url中的一个json,我必须发布内容= html页面的主体?

每次我必须在内容数组中发送一次完成时,你可以在输出中看到html中每行html的输出,即

1 个答案:

答案 0 :(得分:0)

为了发送任何字符串(在您的情况下为HTML字符串),您将不得不使用转义字符。 &#39; /&#39;在JSON的情况下是转义字符。

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

在上面的示例中,我在JSON键中添加了示例html代码&#39; html&#39;并使用&#39; \&#39;作为转义字符忽略双引号(&#34;)。