4
String input = "Vish,Path,123456789";

Expected output as Json string, and thread safe = {"name":"Vish","surname":"Path","mobile":"123456789"}

I tried by using

 GsonBuilder gsonBuilder = new GsonBuilder(); 
 Gson gson = gsonBuilder.create(); 

But every time I'm creating new Object -

MappingObject[] studentArray = new MappingObject[1]; 
studentArray[0] = new MappingObject("Vish","Path","123456789"); 

I separated this comma separated string by using split()

 System.out.println("JSON "+gson.toJson(studentArray));
3
  • 10
    and what problem are you facing? Commented Jul 9, 2019 at 6:35
  • @sidgate I tried by using GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create(); But every time I'm creating new Object - MappingObject[] studentArray = new MappingObject[1]; studentArray[0] = new MappingObject("Vish","Path","123456789"); I separated this comma separated string by using split() System.out.println("JSON "+gson.toJson(studentArray)); So, I want to create single object only even if I work with multiple student and that conversion process should be thread safe as well. Commented Jul 9, 2019 at 6:47
  • 1
    Use JSONObject constructor with Map<String,String> as argument Commented Jul 9, 2019 at 6:51

2 Answers 2

1

You will have to create a Map:

Map<String,String> jsonMap = new HashMap<String,String>();
jsonMap.put("name","Vish");
jsonMap.put("surname","Path");
jsonMap.put("mobile","123456789");

Then use com.google.gson JSONObject: JSONObject jsonObj = new JSONObject(jsonMap);

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

Comments

1

If you don't want to use any library then you have to split string by comma and make a new String.

String input = "Vish,Path,123456789";
String[] values=input.split("[,]");
StringBuffer json = new StringBuffer();// StringBuffer is Thread Safe
json.append("{")
    .append("\"name\": \"").append(values[0]).append("\",")
    .append("\"surname\": \"").append(values[1]).append("\",")
    .append("\"mobile\": \"").append(values[2]).append("\"")
    .append("}");
System.out.println(json.toString());

Output :

{"name": "Vish","surname": "Path","mobile": "123456789"}

If you want to use library then you will achive this by Jackson. Simple make a class and make json by it.

public class Person {
  private String name;
  private String surname;
  private String mobile;

  // ... getters and Setters
}

String input = "Vish,Path,123456789";
String[] values=input.split("[,]");
Person person = new Person(values[0],values[1],values[2]);// Assume you have All Argumets Constructor in specified order  
ObjectMapper mapper = new ObjectMapper(); //com.fasterxml.jackson.databind.ObjectMapper;
String json = mapper.writeValueAsString(person);

Comments

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.