首页 > 解决方案 > 将字节数组解码为字符串格式错误。[爪哇]

问题描述

我的应用程序将推文加载到 Oracle NoSQL 数据库,然后检索它们,并使用 Jackson 库解析 JSON 以获得我需要的值。

我的代码中有两个问题,尽管我很确定一个来自另一个。第一个问题来自于将我从数据库中获取的字节数组 (byte []) 解码为字符串。

这就是 Tweet(一个 JSON 字符串)被编码并保存到数据库中的方式:

Value myValue = Value.createValue(Base64.getEncoder().encode(msg.value().getBytes()));

这会将推文作为字节数组中的值对象保存到数据库中。

现在,我需要检索这个 Value 对象,并获取它存储的字节数组。然后将其解码成一个字符串,最后用 Jackson 解析它。解码是这样完成的:

String data = new String(value.toByteArray(),StandardCharsets.UTF_8);

实际上,这会返回一个字符串。但是那个字符串肯定不是 JSON 格式的推文,而是这个怪物:

……

等等。我在一个辅助项目中进行了一些测试,看看这种转换是否有效并且一切正常。这是用于测试的类:

import java.io.*;
import java.util.*;
import java.math.*;
import bs.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Caca {

static String tocode = "{\"created_at\":\"Tue Apr 23 09:10:22 +0000 2019\",\"id\":1120615888883994624,\"id_str\":\"1120615888883994624\",\"text\":\"RT @chuckwoolery: Largest OIL and GAS find in Historyin West Texas. Dwarfs Saudi, Iran, and Iraq. Did you hear about this In the MSM? NO.\\u2026\",\"source\":\"\\u003ca href=\\\"http:\\/\\/twitter.com\\/download\\/iphone\\\" rel=\\\"nofollow\\\"\\u003eTwitter for iPhone\\u003c\\/a\\u003e\",\"truncated\":false,\"in_reply_to_status_id\":null,\"in_reply_to_status_id_str\":null,\"in_reply_to_user_id\":null,\"in_reply_to_user_id_str\":null,\"in_reply_to_screen_name\":null,\"user\":{\"id\":3545646375,\"id_str\":\"3545646375\",\"name\":\"Tom Willoughby\",\"screen_name\":\"willogus\",\"location\":\"USA\",\"url\":null,\"description\":\"MAJ. USA (Ret) \\u201cDUSTOFF\\u201d\\/\\/Retired-Teacher\\/Administrator\\/ Independent Voter\",\"translator_type\":\"none\",\"protected\":false,\"verified\":false,\"followers_count\":414,\"friends_count\":115,\"listed_count\":1,\"favourites_count\":9322,\"statuses_count\":22038,\"created_at\":\"Fri Sep 04 14:48:46 +0000 2015\",\"utc_offset\":null,\"time_zone\":null,\"geo_enabled\":false,\"lang\":\"en\",\"contributors_enabled\":false,\"is_translator\":false,\"profile_background_color\":\"000000\",\"profile_background_image_url\":\"http:\\/\\/abs.twimg.com\\/images\\/themes\\/theme1\\/bg.png\",\"profile_background_image_url_https\":\"https:\\/\\/abs.twimg.com\\/images\\/themes\\/theme1\\/bg.png\",\"profile_background_tile\":false,\"profile_link_color\":\"4A913C\",\"profile_sidebar_border_color\":\"000000\",\"profile_sidebar_fill_color\":\"000000\",\"profile_text_color\":\"000000\",\"profile_use_background_image\":false,\"profile_image_url\":\"http:\\/\\/pbs.twimg.com\\/profile_images\\/901525176495599621\\/t2G36W62_normal.jpg\",\"profile_image_url_https\":\"https:\\/\\/pbs.twimg.com\\/profile_images\\/901525176495599621\\/t2G36W62_normal.jpg\",\"default_profile\":false,\"default_profile_image\":false,\"following\":null,\"follow_request_sent\":null,\"notifications\":null},\"geo\":null,\"coordinates\":null,\"place\":null,\"contributors\":null,\"retweeted_status\":{\"created_at\":\"Mon Apr 22 13:01:07 +0000 2019\",\"id\":1120311572730191873,\"id_str\":\"1120311572730191873\",\"text\":\"Largest OIL and GAS find in History in West Texas. Dwarfs Saudi, Iran, and Iraq. Did you hear about this In the MSM\\u2026 https:\\/\\/t.co\\/Eptox0vNEx\",\"source\":\"\\u003ca href=\\\"http:\\/\\/twitter.com\\\" rel=\\\"nofollow\\\"\\u003eTwitter Web Client\\u003c\\/a\\u003e\",\"truncated\":true,\"in_reply_to_status_id\":null,\"in_reply_to_status_id_str\":null,\"in_reply_to_user_id\":null,\"in_reply_to_user_id_str\":null,\"in_reply_to_screen_name\":null,\"user\":{\"id\":462104542,\"id_str\":\"462104542\",\"name\":\"Chuck Woolery\",\"screen_name\":\"chuckwoolery\",\"location\":\"Texas\",\"url\":\"http:\\/\\/bluntforcetruth.com\\/\",\"description\":\"Hollywood Conservative, Love Connection Host, Back in Two and Two, Fisherman, Navy Vet, Political Activist. Host of Blunt Force Truth w\\/ @markyoungtruth\",\"translator_type\":\"none\",\"protected\":false,\"verified\":true,\"followers_count\":543465,\"friends_count\":2856,\"listed_count\":3854,\"favourites_count\":9982,\"statuses_count\":75643,\"created_at\":\"Thu Jan 12 15:30:41 +0000 2012\",\"utc_offset\":null,\"time_zone\":null,\"geo_enabled\":true,\"lang\":\"en\",\"contributors_enabled\":false,\"is_translator\":false,\"profile_background_color\":\"200E82\",\"profile_background_image_url\":\"http:\\/\\/abs.twimg.com\\/images\\/themes\\/theme1\\/bg.png\",\"profile_background_image_url_https\":\"https:\\/\\/abs.twimg.com\\/images\\/themes\\/theme1\\/bg.png\",\"profile_background_tile\":false,\"profile_link_color\":\"0084B4\",\"profile_sidebar_border_color\":\"C0DEED\",\"profile_sidebar_fill_color\":\"DDEEF6\",\"profile_text_color\":\"333333\",\"profile_use_background_image\":true,\"profile_image_url\":\"http:\\/\\/pbs.twimg.com\\/profile_images\\/1764172727\\/Chuck_Headshot_normal.jpg\",\"profile_image_url_https\":\"https:\\/\\/pbs.twimg.com\\/profile_images\\/1764172727\\/Chuck_Headshot_normal.jpg\",\"profile_banner_url\":\"https:\\/\\/pbs.twimg.com\\/profile_banners\\/462104542\\/1499952548\",\"default_profile\":false,\"default_profile_image\":false,\"following\":null,\"follow_request_sent\":null,\"notifications\":null},\"geo\":null,\"coordinates\":null,\"place\":null,\"contributors\":null,\"is_quote_status\":false,\"extended_tweet\":{\"full_text\":\"Largest OIL and GAS find in History in West Texas. Dwarfs Saudi, Iran, and Iraq. Did you hear about this In the MSM? NO. Will you? No. If you do it will be the end of the World because it's fossil fuel. Good news for the average American.\",\"display_text_range\":[0,238],\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[],\"symbols\":[]}},\"quote_count\":110,\"reply_count\":181,\"retweet_count\":4868,\"favorite_count\":11615,\"entities\":{\"hashtags\":[],\"urls\":[{\"url\":\"https:\\/\\/t.co\\/Eptox0vNEx\",\"expanded_url\":\"https:\\/\\/twitter.com\\/i\\/web\\/status\\/1120311572730191873\",\"display_url\":\"twitter.com\\/i\\/web\\/status\\/1\\u2026\",\"indices\":[117,140]}],\"user_mentions\":[],\"symbols\":[]},\"favorited\":false,\"retweeted\":false,\"filter_level\":\"low\",\"lang\":\"en\"},\"is_quote_status\":false,\"quote_count\":0,\"reply_count\":0,\"retweet_count\":0,\"favorite_count\":0,\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[{\"screen_name\":\"chuckwoolery\",\"name\":\"Chuck Woolery\",\"id\":462104542,\"id_str\":\"462104542\",\"indices\":[3,16]}],\"symbols\":[]},\"favorited\":false,\"retweeted\":false,\"filter_level\":\"low\",\"lang\":\"en\",\"timestamp_ms\":\"1556010622603\"}";

    static Tweet tw = null;

    public static void main(String[] args) {

        byte[] encoded = Base64.getEncoder().encode(tocode.getBytes());
        byte[] encoded2 = tocode.getBytes(StandardCharsets.UTF_8);
        System.out.println("byte[]encoded is: " + encoded);

        //String decoded = Base64.getDecoder().decode(encoded2).toString();
        String decoded = new String(encoded2,StandardCharsets.UTF_8);
        System.out.println("Decoded is: " + decoded);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        try {
            tw = objectMapper.readValue(decoded, Tweet.class);
        } catch (IOException ex) {
            Logger.getLogger(Caca.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println(tw.toString());
    }
}

第二部分关于杰克逊解析,只要字符串反序列化按预期进行,它就可以正常工作。并且在这个侧面项目中起作用。

关于为什么在主项目上没有正确解码的任何想法?这一切都在 docker 容器上,所以我无法在本地使用实际的 Value 对象进行测试。提前致谢。

标签: javaarraysjsonformatdecoding

解决方案


虽然您应该能够让 base64 编码在这里工作,但您不必这样做。您当前正在从字符串转换为字节,然后通过应用 base64 编码从这些字节转换为另一种形式的字节。假设您可以安全地传输任意字节,使用 base64 只是无缘无故地添加了一层转换。只需将文本转换为字节,指定编码更健壮:

Value myValue = Value.createValue(msg.value().getBytes("UTF-8"));

推荐阅读