将查询字符串参数解析为Java对象 [英] Parse a query string parameter to java object

查看:482
本文介绍了将查询字符串参数解析为Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的查询字符串:

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

我有Java对象:

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

所以我想将此查询字符串解析为该java对象.

我已经搜索并阅读了许多问题,但尚未获得正确的答案.

告诉我什么可以解决这个问题.

解决方案

礼仪

  1. 您确实应该更加详细地说明您尝试过的内容以及为何行不通.

  2. 在这里正确地编写LogObject的代码示例将非常有帮助.

  3. 理想情况下,您应该提供 SSCCE ,以便其他人可以轻松地自己测试问题.

答案

您可以像这样提取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]); 
    }
}

我选择将它们放入HashMap中,但是您可以轻松地查看名称部分(kv[0])并选择对其进行处理.例如:

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

但是,您在LogObject中的所有字段都是私有字段,并且您没有向我们展示任何方法,因此希望您可以从外部设置它们的方式...请记住,您需要将对存储如果您打算使用所有字段初始化LogObject而不是在构造函数调用之后设置字段,则使用某种数据结构(就像我对HashMap所做的那样).

谈到SSCCE,我为此答了一个.

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:

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.

I've searched and read many question but not gotten correct answer yet.

Show me what can solve this problem.

解决方案

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.

  3. Ideally, you would provide a SSCCE so others could easily test your problem themselves.

Answer

You can extract the name:value pairs like this:

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. 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.

Speaking of SSCCEs, I made one for this answer.

这篇关于将查询字符串参数解析为Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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