简体   繁体   English

将查询字符串参数解析为Java对象

[英]Parse a query string parameter to java object

I have query string like that: 我有这样的查询字符串:

ObjectGUId=1abcde&ObjectType=2&ObjectTitle=maximumoflife&Content=racroi&TimeStamp=2012-11-05T17:20:06.056

And I have Java Object: 我有Java对象:

LogObject{
    private String ObjectGUId;
    private String ObjectType;
    private String ObjectTitle;
    private String Content;
    private String TimeStamp;
}

So i want to parse this query string to this java Object. 所以我想将此查询字符串解析为该java对象。

I've searched and read many question but not gotten correct answer yet. 我已经搜索并阅读了许多问题,但尚未获得正确的答案。

Show me what can solve this problem. 告诉我什么可以解决这个问题。

If you do not really need to push the querystring into your own class (you might want that though), instead of parsing it manually, you could use the URLDecoder, as @Sonrobby has commented: 如果您确实不需要将查询字符串放入自己的类中(尽管您可能希望这样做),则可以使用URLDecoder代替手动解析它,就像@Sonrobby所说的那样:

String qString = "ObjectGUId=1abcde&ObjectType=2&ObjectTitle=maximumoflife";
Uri uri = Uri.parse(URLDecoder.decode("http://dummy/?" + qString, "UTF-8"));
if (uri != null) {
    for(String key: uri.getQueryParameterNames()) {
        System.out.println("key=[" + key + "], value=[" + uri.getQueryParameter(key) + "]");
    }
}            

The "dummy" looks dirty but it is required if what you only have is the querystring values (qString). “虚拟”看上去很脏,但是如果您仅有的是查询字符串值(qString),则它是必需的。 If you have the complete URL, just pass it directly to the URLDecoder, and you are done. 如果您具有完整的URL,只需将其直接传递给URLDecoder,就可以完成。

Etiquette 礼仪

  1. You really should be much more specific about what you have tried and why it didn't work. 您确实应该更加详细地说明您尝试过的内容以及为什么它不起作用。

  2. A proper code sample of your LogObject would really be very helpful here. 正确的LogObject代码示例在这里确实很有帮助。

  3. Ideally, you would provide a SSCCE so others could easily test your problem themselves. 理想情况下,您应该提供一个SSCCE,以便其他人可以轻松地测试您自己的问题。

Answer 回答

You can extract the name:value pairs like this: 您可以像这样提取name:value对:

String toParse = "ObjectGUId=1abcde&ObjectType=2&ObjectTitle=maximumoflife&Content=racroi&TimeStamp=2012-11-05T17:20:06.056";
String[] fields = toParse.split("&");
String[] kv;

HashMap<String, String> things = new HashMap<String, String>();


for (int i = 0; i < fields.length; ++i)
{
    t = fields[i].split("=");
    if (2 == kv.length)
    {
        things.put(kv[0], kv[1]); 
    }
}

I have chosen to put them into a HashMap, but you could just as easily look at the name part ( kv[0] ) and choose to do something with it. 我选择将它们放入HashMap中,但是您可以轻松地查看名称部分( kv[0] )并选择对其进行处理。 For example: 例如:

if kv[0].equals("ObjectGUId")
{
    logObject.setGUId(kv[1]); // example mutator/setter method
}
else if //...

However, all your fields in LogObject are private and you haven't shown us any methods, so I hope you have some way of setting them from outside... bear in mind you will need to store the pairs in a data structure of some kind (as I have done with a HashMap ) if you intend to intialise a LogObject with all the fields rather than setting the fields after a constructor call. 但是,您在LogObject中的所有字段都是私有的,并且您没有向我们展示任何方法,因此,我希望您可以从外部设置它们的方式...请记住,您需要将这些对存储在某些数据结构中如果您打算LogObject具有所有字段的LogObject而不是在构造函数调用之后设置字段,则使用这种方法(就像我对HashMap所做的那样)。

Speaking of SSCCEs, I made one for this answer . 说到SSCCE, 我为此做了一个答案

Inspired by @bruno.braga, here's a way using Apache http-components. 受@ bruno.braga的启发,这是使用Apache http-components的一种方法。 You leverage all the parsing corner cases: 您可以利用所有解析极端情况:

List<NameValuePair> params = 
    URLEncodedUtils.parse("http://example.com/?" + queryString, Charset.forName("UTF-8"));

That'll give you a List of NameValuePair objects that should be easy to work with. 这将为您提供易于使用的NameValuePair对象列表。

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

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