解析列表< Map< String,String>>字符串形式再次到POJO [英] Parsing List<Map<String,String>> String form to POJO again

查看:165
本文介绍了解析列表< Map< String,String>>字符串形式再次到POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个来自列表中的 toString()的字符串 object。一些示例代码是:

I am getting a String which comes from calling toString() from a List<Map<String, String>> object. Some sample code is:

List<Map<String,String>> list = new ArrayList<Map<String,String>>();
Map<String, String> map = new HashMap<String, String>();

map.put("key1", "value1");
map.put("key2", "value2");

list.add(map);

String myStr = list.toString();
System.out.println(myStr);

此代码输出:

[{key2=value2, key1=value1}]

为了一个简单的方法来解析这个字符串并再次将其转换为 List< Map< String,String>> 。我只是想知道是否有任何构造函数/实用程序类,在这里很容易做到这一点。我已经看过Java Collections库和Guava从谷歌,但我还没有找到任何已经建立的功能这样做。这是否存在?

I am looking for a simple way to parse this string and convert it to a List<Map<String, String>> again. I am just wondering if there is any constructor / utility class out there to do this easily. I have already look at Java Collections library and Guava from Google but I have not found any function already built to do this. Does this exist?

如果不是,我将创建自己的解析器,但如果存在的话,我会重用一些代码。我喜欢 DRY 的概念。

If not I will just create my own parser but I would to reuse some code if it exists out there. I like the concept of DRY.

推荐答案

一般来说,你不能使用内置的 toString 方法,因为它没有做任何事情值是可解析的,它只是试图给你一个有用的表示。作为一个特别反常的例子,考虑下面的代码,它表明两个不同的地图可以有相同的打印表示:

In general, you can't do this using the built in toString method because it isn't doing anything to make the values parse-able, it's just trying to give you a representation that's helpful. As a particularly perverse example, consider the following code that demonstrates that two distinct maps can have the same printed representation:

import java.util.Map;
import java.util.TreeMap;

public class MapToStringExample {
    public static void main(String[] args) {
        Map<String,String> map1 = new TreeMap<>();
        map1.put( "key1", "value1, key2=value2" );
        System.out.println( "Map1: "+map1 );

        Map<String,String> map2 = new TreeMap<>();
        map2.put( "key1", "value1" );
        map2.put( "key2", "value2" );
        System.out.println( "Map2: "+map2 );
    }
}



Map1: {key1=value1, key2=value2}
Map2: {key1=value1, key2=value2}

如果你想读这样的地图的打印表示,你需要对你可以放入什么值做一些限制(例如,没有包含 = ),或者你需要编写自己的输出例程)。

If you want to read in the printed representation of a Map like this, you'll either need to make some restrictions on what values you can put into it (e.g., nothing containing an = or ,), or you'll need to write your own output routine).

如果你正在寻找避免重写代码,你可以看看序列化API,它将处理序列化和反序列化值。当然,在这种情况下,你只是将字符串映射到字符串,所有你需要的是一种方式来写交替的键和值由一些分隔符分隔,并且一种方法来转义字符串表示中的分隔符(如果允许出现在字符串中;如果不是,那么你甚至不需要转义它)。这不是太难的序列化和反序列化任务。

If you're looking to avoid rewriting code, you might look into the serialization API that would handler serializing and deserializing values for you. Of course, in this case, where you're just mapping strings to strings, all you need is a way to write alternating keys and values separated by some delimiter, and a way to escape that delimiter in the string representation (if it's allowed to appear in strings; if it's not, then you don't even need to escape it). That's not too hard of a serialization and deserialization task.

这篇关于解析列表&lt; Map&lt; String,String&gt;&gt;字符串形式再次到POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆