How to convert String back to Object List in Java
How to convert the string back to object? I have the following class:
class Test {
String name;
String value;
String testing;
@Override
public String toString() {
return "Test{" +
"name='" + name + ''' +
", value='" + value + ''' +
", testing='" + testing + ''' +
'}';
}
}
class Testing {
private List<Test> testing = new ArrayList<Test>();
handling(testing.toString())
public String handling(String testing) {
// do some handling and return a string
}
}
ArrayList testing
must handled by convert to string
, for example, after that, we get the following string:
[Test{name='name1', value='value1', testing='testing1'}, Test{name='name2', value='value2', testing='testing2'}, Test{name='name3', value='value3', testing='testing3'}]
then how to convert the string back to object list of Test
?
Can anyone help on it?
java string list
add a comment |
How to convert the string back to object? I have the following class:
class Test {
String name;
String value;
String testing;
@Override
public String toString() {
return "Test{" +
"name='" + name + ''' +
", value='" + value + ''' +
", testing='" + testing + ''' +
'}';
}
}
class Testing {
private List<Test> testing = new ArrayList<Test>();
handling(testing.toString())
public String handling(String testing) {
// do some handling and return a string
}
}
ArrayList testing
must handled by convert to string
, for example, after that, we get the following string:
[Test{name='name1', value='value1', testing='testing1'}, Test{name='name2', value='value2', testing='testing2'}, Test{name='name3', value='value3', testing='testing3'}]
then how to convert the string back to object list of Test
?
Can anyone help on it?
java string list
Not sure why you really need this, since you have the originalList<Test>
within scope, but you could always add a static utility method to yourTest
class that would de-serialize a givenString
representation back into aTest
instance.
– Mena
Nov 19 '18 at 13:38
3
If this is for some kind of text-based api I'd recommend using JSON which is easier to parse especially since there are a lot of libraries for that. If you use your own custom text representation then you'll have to provide your own custom parser as well.
– Thomas
Nov 19 '18 at 13:43
2
You shouldn't use toString() to serialize your objects. Instead of this just use Jackson to serialize objects to JSON and back from JSON to POJO. You objects should implement Serializable for it.
– Vlad
Nov 19 '18 at 13:43
it is not Json actually
– ratzip
Nov 19 '18 at 13:44
No, it is not JSON, but it could/should be. Otherwise you have to write a parser for it yourself.
– tevemadar
Nov 19 '18 at 13:45
add a comment |
How to convert the string back to object? I have the following class:
class Test {
String name;
String value;
String testing;
@Override
public String toString() {
return "Test{" +
"name='" + name + ''' +
", value='" + value + ''' +
", testing='" + testing + ''' +
'}';
}
}
class Testing {
private List<Test> testing = new ArrayList<Test>();
handling(testing.toString())
public String handling(String testing) {
// do some handling and return a string
}
}
ArrayList testing
must handled by convert to string
, for example, after that, we get the following string:
[Test{name='name1', value='value1', testing='testing1'}, Test{name='name2', value='value2', testing='testing2'}, Test{name='name3', value='value3', testing='testing3'}]
then how to convert the string back to object list of Test
?
Can anyone help on it?
java string list
How to convert the string back to object? I have the following class:
class Test {
String name;
String value;
String testing;
@Override
public String toString() {
return "Test{" +
"name='" + name + ''' +
", value='" + value + ''' +
", testing='" + testing + ''' +
'}';
}
}
class Testing {
private List<Test> testing = new ArrayList<Test>();
handling(testing.toString())
public String handling(String testing) {
// do some handling and return a string
}
}
ArrayList testing
must handled by convert to string
, for example, after that, we get the following string:
[Test{name='name1', value='value1', testing='testing1'}, Test{name='name2', value='value2', testing='testing2'}, Test{name='name3', value='value3', testing='testing3'}]
then how to convert the string back to object list of Test
?
Can anyone help on it?
java string list
java string list
asked Nov 19 '18 at 13:36
ratzip
4571721
4571721
Not sure why you really need this, since you have the originalList<Test>
within scope, but you could always add a static utility method to yourTest
class that would de-serialize a givenString
representation back into aTest
instance.
– Mena
Nov 19 '18 at 13:38
3
If this is for some kind of text-based api I'd recommend using JSON which is easier to parse especially since there are a lot of libraries for that. If you use your own custom text representation then you'll have to provide your own custom parser as well.
– Thomas
Nov 19 '18 at 13:43
2
You shouldn't use toString() to serialize your objects. Instead of this just use Jackson to serialize objects to JSON and back from JSON to POJO. You objects should implement Serializable for it.
– Vlad
Nov 19 '18 at 13:43
it is not Json actually
– ratzip
Nov 19 '18 at 13:44
No, it is not JSON, but it could/should be. Otherwise you have to write a parser for it yourself.
– tevemadar
Nov 19 '18 at 13:45
add a comment |
Not sure why you really need this, since you have the originalList<Test>
within scope, but you could always add a static utility method to yourTest
class that would de-serialize a givenString
representation back into aTest
instance.
– Mena
Nov 19 '18 at 13:38
3
If this is for some kind of text-based api I'd recommend using JSON which is easier to parse especially since there are a lot of libraries for that. If you use your own custom text representation then you'll have to provide your own custom parser as well.
– Thomas
Nov 19 '18 at 13:43
2
You shouldn't use toString() to serialize your objects. Instead of this just use Jackson to serialize objects to JSON and back from JSON to POJO. You objects should implement Serializable for it.
– Vlad
Nov 19 '18 at 13:43
it is not Json actually
– ratzip
Nov 19 '18 at 13:44
No, it is not JSON, but it could/should be. Otherwise you have to write a parser for it yourself.
– tevemadar
Nov 19 '18 at 13:45
Not sure why you really need this, since you have the original
List<Test>
within scope, but you could always add a static utility method to your Test
class that would de-serialize a given String
representation back into a Test
instance.– Mena
Nov 19 '18 at 13:38
Not sure why you really need this, since you have the original
List<Test>
within scope, but you could always add a static utility method to your Test
class that would de-serialize a given String
representation back into a Test
instance.– Mena
Nov 19 '18 at 13:38
3
3
If this is for some kind of text-based api I'd recommend using JSON which is easier to parse especially since there are a lot of libraries for that. If you use your own custom text representation then you'll have to provide your own custom parser as well.
– Thomas
Nov 19 '18 at 13:43
If this is for some kind of text-based api I'd recommend using JSON which is easier to parse especially since there are a lot of libraries for that. If you use your own custom text representation then you'll have to provide your own custom parser as well.
– Thomas
Nov 19 '18 at 13:43
2
2
You shouldn't use toString() to serialize your objects. Instead of this just use Jackson to serialize objects to JSON and back from JSON to POJO. You objects should implement Serializable for it.
– Vlad
Nov 19 '18 at 13:43
You shouldn't use toString() to serialize your objects. Instead of this just use Jackson to serialize objects to JSON and back from JSON to POJO. You objects should implement Serializable for it.
– Vlad
Nov 19 '18 at 13:43
it is not Json actually
– ratzip
Nov 19 '18 at 13:44
it is not Json actually
– ratzip
Nov 19 '18 at 13:44
No, it is not JSON, but it could/should be. Otherwise you have to write a parser for it yourself.
– tevemadar
Nov 19 '18 at 13:45
No, it is not JSON, but it could/should be. Otherwise you have to write a parser for it yourself.
– tevemadar
Nov 19 '18 at 13:45
add a comment |
2 Answers
2
active
oldest
votes
If you don't need exactly that toString
pattern, but only need to convert your Object
to something human-readable and than back to an Object
, you could marshal to json and parse back to Object
seamlessly with something like Jackson ObjectMapper
. See https://www.baeldung.com/jackson-object-mapper-tutorial for a quick-start.
add a comment |
You can create an additional constructor and call them with the string:
class Test {
private String name;
private String value;
private String testing;
Test(string objString) {
Pattern pattern = Pattern.compile("name=('.+'), value=('.+), testing=('.+')");
Matcher matcher = pattern.matcher(objString);
if (matcher.matches()) {
name = matcher.group(1);
value = matcher.group(2);
testing = matcher.group(3);
} else {
throw new ArgumentException("Cannot parse""):
}
}
@Override
public String toString() {
return "Test{" +
"name='" + name + ''' +
", value='" + value + ''' +
", testing='" + testing + ''' +
'}';
}
}
class Testing {
doTesting(List<Test> testing) {
List<String> testingAsString = new ArrayList<String>();
// To string
for (Test t : testing) {
testingAsString.add(t.toString());
}
List<Test> clonedTesting = new ArrayList<Test>();
// To Test
for (String t : testingAsString) {
clonedTesting.add(new Test(t));
}
// Here every test string is again an object of type Test
for (Test t : clonedTesting) {
System.out.println(t);
}
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375830%2fhow-to-convert-string-back-to-object-list-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you don't need exactly that toString
pattern, but only need to convert your Object
to something human-readable and than back to an Object
, you could marshal to json and parse back to Object
seamlessly with something like Jackson ObjectMapper
. See https://www.baeldung.com/jackson-object-mapper-tutorial for a quick-start.
add a comment |
If you don't need exactly that toString
pattern, but only need to convert your Object
to something human-readable and than back to an Object
, you could marshal to json and parse back to Object
seamlessly with something like Jackson ObjectMapper
. See https://www.baeldung.com/jackson-object-mapper-tutorial for a quick-start.
add a comment |
If you don't need exactly that toString
pattern, but only need to convert your Object
to something human-readable and than back to an Object
, you could marshal to json and parse back to Object
seamlessly with something like Jackson ObjectMapper
. See https://www.baeldung.com/jackson-object-mapper-tutorial for a quick-start.
If you don't need exactly that toString
pattern, but only need to convert your Object
to something human-readable and than back to an Object
, you could marshal to json and parse back to Object
seamlessly with something like Jackson ObjectMapper
. See https://www.baeldung.com/jackson-object-mapper-tutorial for a quick-start.
answered Nov 19 '18 at 13:45
Davide
1125
1125
add a comment |
add a comment |
You can create an additional constructor and call them with the string:
class Test {
private String name;
private String value;
private String testing;
Test(string objString) {
Pattern pattern = Pattern.compile("name=('.+'), value=('.+), testing=('.+')");
Matcher matcher = pattern.matcher(objString);
if (matcher.matches()) {
name = matcher.group(1);
value = matcher.group(2);
testing = matcher.group(3);
} else {
throw new ArgumentException("Cannot parse""):
}
}
@Override
public String toString() {
return "Test{" +
"name='" + name + ''' +
", value='" + value + ''' +
", testing='" + testing + ''' +
'}';
}
}
class Testing {
doTesting(List<Test> testing) {
List<String> testingAsString = new ArrayList<String>();
// To string
for (Test t : testing) {
testingAsString.add(t.toString());
}
List<Test> clonedTesting = new ArrayList<Test>();
// To Test
for (String t : testingAsString) {
clonedTesting.add(new Test(t));
}
// Here every test string is again an object of type Test
for (Test t : clonedTesting) {
System.out.println(t);
}
}
}
add a comment |
You can create an additional constructor and call them with the string:
class Test {
private String name;
private String value;
private String testing;
Test(string objString) {
Pattern pattern = Pattern.compile("name=('.+'), value=('.+), testing=('.+')");
Matcher matcher = pattern.matcher(objString);
if (matcher.matches()) {
name = matcher.group(1);
value = matcher.group(2);
testing = matcher.group(3);
} else {
throw new ArgumentException("Cannot parse""):
}
}
@Override
public String toString() {
return "Test{" +
"name='" + name + ''' +
", value='" + value + ''' +
", testing='" + testing + ''' +
'}';
}
}
class Testing {
doTesting(List<Test> testing) {
List<String> testingAsString = new ArrayList<String>();
// To string
for (Test t : testing) {
testingAsString.add(t.toString());
}
List<Test> clonedTesting = new ArrayList<Test>();
// To Test
for (String t : testingAsString) {
clonedTesting.add(new Test(t));
}
// Here every test string is again an object of type Test
for (Test t : clonedTesting) {
System.out.println(t);
}
}
}
add a comment |
You can create an additional constructor and call them with the string:
class Test {
private String name;
private String value;
private String testing;
Test(string objString) {
Pattern pattern = Pattern.compile("name=('.+'), value=('.+), testing=('.+')");
Matcher matcher = pattern.matcher(objString);
if (matcher.matches()) {
name = matcher.group(1);
value = matcher.group(2);
testing = matcher.group(3);
} else {
throw new ArgumentException("Cannot parse""):
}
}
@Override
public String toString() {
return "Test{" +
"name='" + name + ''' +
", value='" + value + ''' +
", testing='" + testing + ''' +
'}';
}
}
class Testing {
doTesting(List<Test> testing) {
List<String> testingAsString = new ArrayList<String>();
// To string
for (Test t : testing) {
testingAsString.add(t.toString());
}
List<Test> clonedTesting = new ArrayList<Test>();
// To Test
for (String t : testingAsString) {
clonedTesting.add(new Test(t));
}
// Here every test string is again an object of type Test
for (Test t : clonedTesting) {
System.out.println(t);
}
}
}
You can create an additional constructor and call them with the string:
class Test {
private String name;
private String value;
private String testing;
Test(string objString) {
Pattern pattern = Pattern.compile("name=('.+'), value=('.+), testing=('.+')");
Matcher matcher = pattern.matcher(objString);
if (matcher.matches()) {
name = matcher.group(1);
value = matcher.group(2);
testing = matcher.group(3);
} else {
throw new ArgumentException("Cannot parse""):
}
}
@Override
public String toString() {
return "Test{" +
"name='" + name + ''' +
", value='" + value + ''' +
", testing='" + testing + ''' +
'}';
}
}
class Testing {
doTesting(List<Test> testing) {
List<String> testingAsString = new ArrayList<String>();
// To string
for (Test t : testing) {
testingAsString.add(t.toString());
}
List<Test> clonedTesting = new ArrayList<Test>();
// To Test
for (String t : testingAsString) {
clonedTesting.add(new Test(t));
}
// Here every test string is again an object of type Test
for (Test t : clonedTesting) {
System.out.println(t);
}
}
}
edited Nov 19 '18 at 13:57
answered Nov 19 '18 at 13:51
Kevin Wallis
3,07031533
3,07031533
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375830%2fhow-to-convert-string-back-to-object-list-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Not sure why you really need this, since you have the original
List<Test>
within scope, but you could always add a static utility method to yourTest
class that would de-serialize a givenString
representation back into aTest
instance.– Mena
Nov 19 '18 at 13:38
3
If this is for some kind of text-based api I'd recommend using JSON which is easier to parse especially since there are a lot of libraries for that. If you use your own custom text representation then you'll have to provide your own custom parser as well.
– Thomas
Nov 19 '18 at 13:43
2
You shouldn't use toString() to serialize your objects. Instead of this just use Jackson to serialize objects to JSON and back from JSON to POJO. You objects should implement Serializable for it.
– Vlad
Nov 19 '18 at 13:43
it is not Json actually
– ratzip
Nov 19 '18 at 13:44
No, it is not JSON, but it could/should be. Otherwise you have to write a parser for it yourself.
– tevemadar
Nov 19 '18 at 13:45