Serializes STRING into JSON Format

I’m trying to Serialize a string into JSON content but does not seems its JSON content and I have tried the following

Is there a way to convert to JSON format or you have do manually?

String jsonStr = '[image[1,2,3,4];image2:[2,23];image3:[3,4];image4:[true];image5:[true];image6:[true];]';

String JSONString = JSON.serialize(jsonStr);
system.debug('JSONString : ' +JSONString );

after executing the above in Anonymous Window I get the following result:

“[image[1,2,3,4];image2:[2,23];image3:[3,4];image4:[true];image5:[true];image6:[true];]”

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It’s not clear what the output String you are expecting is.
The contents of the String are JSON so perhaps you are wanting to expand those into an object?

If you can express what you think the output string should look like, then we may be able to better help? You want an output that is a String, but somehow changed from the original String?

Perhaps you are looking for something like this

public class AWrapper{  
    String jsonStr;
}

aWrapper nickWrapper = new AWrapper();
nickWrapper.jsonStr = '[image[1,2,3,4];image2:[2,23];image3:[3,4];image4:[true];image5:[true];image6:[true];]';
String JSONString = JSON.serialize(NickWrapper);
system.debug('JSONString : ' +JSONString );

So the output looks like

{"jsonStr":"[image[1,2,3,4];image2:[2,23];image3:[3,4];image4:[true];image5:[true];image6:[true];]"}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x