【问题标题】:Structure JSOUP content to JSON将 JSOUP 内容结构化为 JSON
【发布时间】:2017-03-23 14:45:44
【问题描述】:

我从 http 站点提取了一些数据,现在我想使用 JSON 对其进行格式化。我看了一些例子,但我不明白该怎么做。我想要这样的东西:

{  
  Product name:"Samsung..."
    { review 1:"..."
      review 2:"..."
   }
}

此外,我的代码不会遍历所有评论,它会在第 1 页停止,还有 10 页,我希望至少获得 20-30 条评论。到目前为止我所做的就是:

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsoupMain {

private static int nr = 1;

public static void main(String[] args) {

    Document document = null;
    try {
        document = Jsoup.connect("http://www.emag.ro/telefon-mobil-samsung- galaxy-j5-2016-dual-sim-16gb-4g-gold-sm-j510fzdurom/pd/DQD3B7BBM/").get();

        String title = document.title();
        System.out.println("Title: " + title);

        Elements review = document.select("div.product-review-body");

        for (Element rev : review) {
            System.out.println("Review : " + nr + " :" + rev.text() + "\n");
            nr++;
        }

    } catch (IOException e) {

        e.printStackTrace();
    }

}

}

【问题讨论】:

    标签: java json jsoup


    【解决方案1】:

    查看此答案以了解类似问题 - Parsing table html with Jsoup 在您的情况下,它可能如下所示:

        JSONObject root = new JSONObject();
        JSONArray reviews = new JSONArray();
        root.put("name", "Product Name");
        for (int i=0; i < elements.size(); i++){
            JSONObject review = new JSONObject();
            review.put("review"+i , elements.get(i).text());
            reviews.add(review);
        }
        root.put("reviews", reviews);
    

    root 的 JSON 输出如下所示:

    {
       "name":"Product Name",
       "reviews":[
          {
             "review0":"ok"
          },
          {
             "review1":"awesome"
          }
       ]
    }
    

    【讨论】:

    • JSONArray 没有定义 add 方法,eclipse 是这么说的
    • 您使用的是什么版本的 'json-lib'?上面的代码就是基于这个依赖——“net.sf.json-lib:json-lib:2.4:jdk15”。
    • 我在那个时候解决了这个问题,但它仍然没有像我想要的那样显示信息。我的意思是评论是一个在另一个之下。我会附上我的代码..但我不知道我使用什么版本的 JSON 库,老实说我不知道​​在哪里可以找到它
    • 而不是使用 JSONArray,只需将 JSONObjects 放在您的主 JSONObject 中。请注意,您所需的格式示例是无效的 JSON。 json.org 上有效格式的结帐规则
    • 我看到它可以转换成一些漂亮的打印 JSON 数组,但我不明白它是如何工作的
    【解决方案2】:

    我想让这段代码显示完全一样的信息:

     { Reviews:
       {  review 1: "..."}
       {  review 2: "..."}
         ................
         ................
     }
    

    目前我的代码正在打印类似这样的信息:“评论”:[[{“评论 0”:“Foarte multumit Un telefon foarte bun。Ce m-a impresionat este camera foto care face niste poze foarte bune”},{“ R............}

    import java.io.IOException;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    public class JsoupMain {
    
    private static int nr = 1;
    
    public static void main(String[] args) {
    
        Document document = null;
        try {
            document = Jsoup.connect("http://www.emag.ro/telefon-mobil-samsung-galaxy-j5-2016-dual-sim-16gb-4g-gold-sm-j510fzdurom/pd/DQD3B7BBM/").get();
    
            String title = document.title();
            System.out.println("Product name : " + title);
    
            Elements review = document.select("div.product-review-body");
    
            JSONObject mainObject = new JSONObject();
            JSONObject root = new JSONObject();
            JSONArray list = new JSONArray();
            root.put("Product name", title);
            for (int i = 0; i < review.size(); i++) {
                JSONObject revieww = new JSONObject();
                revieww.put("Review " + i, review.get(i).text());
                list.put(revieww);
    
            }
    
            mainObject.accumulate("Reviews", list);
            System.out.println(mainObject);
    
        } catch (IOException e) {
    
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
    

    }

    【讨论】:

      【解决方案3】:

      将您的代码从 Elements review = document.select("div.product-review-body");... 替换为 ... System.out.println(mainObject); 为以下代码 sn-p。

                  Elements review = document.select("div[class=product-review-body]");
                  JSONObject parent = new JSONObject();
                  parent.put("Product name", title);
                  JSONArray reviews = new JSONArray();
                  int i = 1;
                  for (Element rev : review) {
                      reviews.put(new JSONObject().put("review" + i, rev.text()));
                      i++;
                  }
                  parent.put("Reviews", reviews);
                  System.out.println(parent);
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2020-07-08
        • 1970-01-01
        • 1970-01-01
        • 2019-12-09
        • 2019-02-21
        • 1970-01-01
        • 1970-01-01
        • 2017-11-26
        • 1970-01-01
        相关资源
        最近更新 更多