集册 Java实例教程 将JSON对象解析为链接映射。

将JSON对象解析为链接映射。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

479
将JSON对象解析为链接映射。

/*

 * ClientUtilities

 * Copyright (C) 2015 Nishimura Software Studio

 *

 * This program is free software: you can redistribute it and/or modify it

 * under the terms of the GNU Affero General Public License as published by the

 * Free Software Foundation, either version 3 of the License, or (at your

 * option) any later version.

 *

 * This program is distributed in the hope that it will be useful, but WITHOUT

 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or

 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License

 * for more details.

 *

 * You should have received a copy of the GNU Affero General Public License

 * along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */// from N o w J a v a . c o m

import java.net.MalformedURLException;

import java.net.URL;

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

import java.util.UUID;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.json.JsonObject;

import javax.json.JsonValue;


public class Main{

    private static final String PACKAGE_NAME = ClientUtilities.class

            .getPackage().getName();
            /** 
             来自 时 代 J a v a 公 众 号 - nowjava.com**/

    /**

     * Parses a JSON object into a map of links.

     * @param jsonObject JSON object

     * @return map of links

     */

    public static Map<String, URL> parseLinks(JsonObject jsonObject) {

        Map<String, URL> links = new HashMap<String, URL>();

        for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) {

            try {

                URL link = parseLink((JsonObject) entry.getValue());

                links.put(entry.getKey(), link);

            } catch (MalformedURLException exception) {

                getLogger().log(Level.WARNING,

                        "Could not parse a URL value", exception);

            }

        }

        return links;

    }

    /**

     * Parses a JSON object into a URL.

     * @param jsonObject JSON object

     * @return link

     * @throws MalformedURLException if the <code>href</code> value could not

     * be parsed as a URL.

     */

    protected static URL parseLink(JsonObject jsonObject)

            throws MalformedURLException {

        return parseURL(jsonObject.getString(ClientJsonKeys.HREF));

    }

    /**

     * Returns a logger.

     * @return logger for this package

     */

    public static Logger getLogger() {

        return Logger.getLogger(PACKAGE_NAME, PACKAGE_NAME

                + ".resources.LogMessages");

    }

    
展开阅读全文