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

时间:2019-05-03 11:09:09

标签: java arrays json format decoding

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

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

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

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

这会将tweet作为字节数组中的Value object保存到数据库中。

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

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

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

  

... eyJjcmVhdGVkX2F0IjoiRnJpIE1heSAwMyAxMDozOToyNCArMDAwMCAyMDE5IiwiaWQiOjExMjQyNjIxNzQ1Njg5MjcyMzIsImlkX3N0ciI6IjExMjQyNjIxNzQ1Njg5MjcyMzIiLCJ0ZXh0IjoiUG9tcGVvOiBPbWFyXHUyMDE5cyBWZW5lenVlbGEgY29tbWVudHMgXHUyMDE4ZGlzZ3VzdGluZ1x1MjAxOSBcdTIwMTMgRm94XHUwMGEwTmV3cyBodHRwczpcL1wvdC5jb1wvNDY2UTNZS1pLeiBodHRwczpcL1wvdC5jb1wvNGRKSFpWeVJiMCIsImRpc3BsYXlfdGV4dF9yYW5nZSI6WzAsODFdLCJzb3VyY2UiOiJcdTAwM2NhIGhyZWY9XCJodHRwOlwvXC9wdWJsaWNpemUud3AuY29tXC9cIiByZWw9XCJub2ZvbGxvd1wiXHUwMDNlV29yZFByZXNzLmNvbVx1MDAzY1wvYVx1MDAzZSIsInRydW5jYXRlZCI6ZmFsc2UsImluX3JlcGx5X3RvX3N0YXR1c19pZCI6bnVsbCwiaW5fcmVwbHlfdG9fc3RhdHVzX2lkX3N0ciI6bnVsbCwiaW5fcmVwbHlfdG9fdXNlcl9pZCI6bnVsbCwiaW5

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

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对象。预先感谢。

1 个答案:

答案 0 :(得分:5)

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

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