繁体   English   中英

将字节数组解码为String格式错误。 [Java]

[英]Decoding byte array into String format error. [Java]

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

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

这是Tweet(JSON字符串)的编码方式,并将其保存到数据库中:

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

这会将tweet作为字节数组中的Value对象保存到DB中。

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

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

实际上,这将返回一个String。 但是那个String肯定不是JSON格式的Tweet,而是这个怪物:

....

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

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());
    }
}

第二部分涉及Jackson解析,只要String反序列化按预期进行,它就可以正常工作。 并在这一方面的项目工作。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM