JSON

对比分析json及XML【站长博客网】(4)

字号+ 作者:H5之家 来源:H5之家 2017-05-12 13:06 我要评论( )

public ResultInfo onParser(Element rootElement) { int resp = -1; try { String elName = "header/respcode"; resp = Integer.parseInt(selectNodeString(rootElement, elName)); } catch (NumberFormatExceptio


public ResultInfo onParser(Element rootElement) {
    int resp = -1;
    try {
        String elName = "header/respcode";
        resp = Integer.parseInt(selectNodeString(rootElement, elName));
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
    Log.d(TAG, "resp= " + resp);
    if (resp != 0) {
        return null;
    }
    ResultInfo searchResultInfo = new ResultInfo();
    // Parse Search Weight
    @SuppressWarnings("rawtypes")
    final List weights = rootElement.selectNodes(rootElement.getPath() + "http://www.350351.com/"
            + "result/weights/weight");
    ResultInfo[] resultFilterInfos = parseVideos(weights);
    if (resultFilterInfos != null) {
        ResultInfo weight = new ResultInfo();
        weight.putResultInfoArray(ResultInfo.KEY_VIDEOS, resultFilterInfos);
        searchResultInfo.putResultInfo(ResultInfo.KEY_WEIGHT, weight);
    }
    // Parse Albums
    @SuppressWarnings("rawtypes")
    final List albums = rootElement.selectNodes(rootElement.getPath() + "http://www.350351.com/"
            + "result/albums/album");
    ResultInfo[] resultInfos = parseVideos(albums);
    if (resultInfos != null) {
        ResultInfo album = new ResultInfo();
        album.putResultInfoArray(ResultInfo.KEY_VIDEOS, resultInfos);
        searchResultInfo.putResultInfo(ResultInfo.KEY_SEARCH, album);
    }
    return searchResultInfo;
}

  1.7.2 Step 2

  找到了对应的Node,即从对应的Node开始递归的查找,直到找到最小的节点,也就是最基本的单元Element。再对每一个Element进行解析:

复制代码 代码如下:


private ResultInfo[] parseVideos(final List nodes) {
    if (nodes != null && nodes.size() > 0) {
        final int size = nodes.size();
        final ResultInfo[] vis = new ResultInfo[size];
        int i = 0;
        for (Object o : nodes) {
            if (o instanceof Element) {
                final Element videoElement = (Element) o;
                ResultInfo vi = parseVideo(videoElement);
                vis[i] = vi;
            }
            i++;
        }
        return vis;
    }
    return null;
}

  1.7.3 Step 3

  针对获取到的Element,解析出对应的String将数据传递给VideoInfo这个类:

复制代码 代码如下:


private ResultInfo parseVideo(final Element videoElement) {
    final String id = videoElement.elementText("album_id");
    final String title = videoElement.elementText("title");
    final String categoryId = videoElement.elementText("category_id");
    final String categoryName = videoElement.elementText("category_name");
    final String count = videoElement.elementText("count");
    final String imgUrl = videoElement.elementText("img180236");
    final String duration = videoElement.elementText("duration");
    final String mainactors = videoElement.elementText("mainactors");
    final String sitename = videoElement.elementText("site_name");
    final String videourl = videoElement.elementText("vedio_url");
    final String sort = videoElement.elementText("sort");
    final String tv_id = videoElement.elementText("tv_id");
    ResultInfo vi = new ResultInfo();
    vi.putString(VideoInfo.ID, id);
    vi.putString(VideoInfo.TITLE, title);
    vi.putString(VideoInfo.CATEGORY_ID, categoryId);
    vi.putString(VideoInfo.CATEGORY_NAME, categoryName);
    vi.putString(VideoInfo.COUNT, count);
    vi.putString(VideoInfo.IMG_URL, imgUrl);
    vi.putString(VideoInfo.DURATION, duration);
    vi.putString(VideoInfo.MAINACTORS, mainactors);
    vi.putString(VideoInfo.SITENAME, sitename);
    vi.putString(VideoInfo.VIDEOURL, videourl);
    vi.putString(VideoInfo.SORT, sort);
    vi.putString(VideoInfo.TV_ID, tv_id);
    return vi;
}


  1.7.4 Step 4

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • json对日期的处置

    json对日期的处置

    2017-05-12 14:05

  • Python学习笔记之解析json的方法分析【站长博客网】

    Python学习笔记之解析json的方法分析【站长博客网】

    2017-05-10 17:07

  • iis支持json 配置iis支持json格式的文件教材

    iis支持json 配置iis支持json格式的文件教材

    2017-05-10 12:00

  • 如何解析DELPHI XE5服务器返回的JSON数据(翻译)及中文乱码

    如何解析DELPHI XE5服务器返回的JSON数据(翻译)及中文乱码

    2017-05-10 11:15

网友点评