1

From my service I am generating the JSON based on the language.I need to covert the array to the map which contains one value as key and another as value (lot of value in the arrays based on the database).

I have written the code I am getting the array and only "I need to convert into the list of key value pair like the given sample and write in the text file.

JSON from my service

[
[
    "task",
    "Comments"
],
[
    "CUSTOM_43_01",
    "Email"
],
[
    "CUSTOM_44_02",
    "Mobile"
],..........
]
5
  • I think it would happen if you returned a Map instead of a LIst. Commented Jan 7, 2019 at 17:45
  • display LanguageDTO properties please! Commented Jan 7, 2019 at 17:48
  • Your expected response is not a valid JSON. Commented Jan 7, 2019 at 17:55
  • @HadiJ i have added the DTO's Commented Jan 8, 2019 at 3:27
  • As per the previous comments, A map would get you closer to your result. Also a map literal would separate key/value with a colon instead of an equal. See stackoverflow.com/q/443499/744133. Commented Jan 8, 2019 at 3:32

1 Answer 1

1

Try to convert your result from repository to expected result in the service like bellow:

public Map<String,String> getAllLangaugeDataForEn(Integer appId, String language) {
  Map<String,String> result = new HashMap();
  if (language.equalsIgnoreCase("EN")) {
   result  = languageRepository.getEnLanguageList(appId)
            .stream()
            .map(language-> new AbstractMap.SimpleEntry<>(language.getX(),language.getY()))
            .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
   }
     return result;
}

language.getX() and language.getY() are LanguageDTO getters methods(LanguageDTO properties)!

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

5 Comments

I am getting The method stream() is undefined for the type Map<String,String> error
I have added my DTO
well, you tagged java-8 due to this reason I put this answer. what is your java version?
java version is java -8
thelanguageRepository.getEnLanguageList(appId) result is list so stream() is defined for it. can you display or say in detail your error?

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.