0

How do I loop over a string of item and create an object based on that?

I currently have this code:

public static Object ParseParams(String string)
{
    Object params = new Object();
    String[] lines = string.split("\n");
    for(String line : lines)
    {
        String[] splittedLine = line.split("=");
        params[splittedLine[0]] = splittedLine[1]; //JavaScript syntax, not Java!
    }
    return params;
}

The input string is in this format:

param1=value1
param2=value2
foo=bar

How do I fix the problematic line?

Edit

Sometimes the string would look like this:

foo=bar
param=1=hello
param=2=world

Would it be possible with Maps in Java to get the output like this:

foo
  bar
param
  1
    hello
  2
    world

So the Maps are sometimes nested, and it you would retrieve hello by calling params.get("param").get("1");

1
  • 1
    Oh, that's not how objects in Java work at all. You may want to stick with a Map implementation like HashMap, or use a JSON library if that's your end goal. Commented Oct 31, 2011 at 23:47

1 Answer 1

3

It sounds like you want either a Map, or a JSON library.

Maps

public static Map<String, String> ParseParams(String string)
{
    Map<String, String> params = new HashMap<String, String>();
    String[] lines = string.split("\n");
    for(String line : lines)
    {
        String[] splittedLine = line.split("=");
        params.put(splittedLine[0], splittedLine[1]);
    }
    return params; // Get a param with params.get(key);
}

In general you'll want a HashMap (fast but not stored in order), but there's also a TreeMap which is slightly slower but stored in order (which can be useful sometimes).

JSON

The format JavaScript uses for objects is used as a general-purpose storage format called JSON (JavaScript Object Notation). This is only useful for storage / printing / network transmission. Internally, Java JSON libraries use maps (JavaScript interpreters probably do too).

There are several Java APIs, but StackOverflow users seem to recommend Json-lib:

public static JSONObject ParseParams(String string)
{
    // Note that we need everything from the other method anyway
    return JSONObject.fromObject(ParseParams(string));
}

EDIT:

You're already reaching the point where using a Map is strained. I'd suggest just using a class:

class MyStuff {
    String foo;
    Map<String, String> params;
}

It is possible to nest Maps, like Map<String, Map<String, String>> or Map<String, Object>, but you really should be using classes for this.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much, could you please take a look at my edit of the question?
<noobQuestion>How would you do that with classes?</noobQuestion>
@Tylio - Sorry, I don't think I could give a decent introduction to classes. You could try the Java Tutorial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.