解析列表& lt; Map& lt; String,String& gt;& gt;再次将字符串形式转换为POJO [英] Parsing List<Map<String,String>> String form to POJO again

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

问题描述

我得到一个String,它来自 List< Map< String,String>> 对象中的调用 toString().一些示例代码是:

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>> 的简单方法.我只是想知道是否有任何构造函数/实用程序类可以轻松地做到这一点.我已经看过Google的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,该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.

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

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