Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7550925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:20:39+00:00 2026-05-30T10:20:39+00:00

I have a mapping from Strings to List of Strings in Java: 100 ->

  • 0

I have a mapping from Strings to List of Strings in Java:

"100"  -> ["New York"]
"200"  -> ["Geneva", "Tel Aviv"]
"300"  -> ["Rome", "Paris", "Atlanta"]
"400a" -> ["Los Angeles"]

I want to convert this to JSON using gson. 99% of the keys are Integers, but typos (like 400a can occur. My desired output is:

{
    100    : ["New York"], 
    200    : ["Geneva", "Tel Aviv"],
    300    : ["Rome", "Paris", "Atlanta"],
    "400a" : ["Los Angeles"]
}

Note that "400a" is a String, and 100, 200, 300 are integers.

How can I convert my map in an elegant and efficient manner?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:20:40+00:00Added an answer on May 30, 2026 at 10:20 am

    What you are trying to do is not a valid json. See the specification: http://www.json.org/. The attribute names should always be strings, numbers are not allowed. If you then want to obtain the following json:

    {
        "100"  : ["New York"], 
        "200"  : ["Geneva", "Tel Aviv"],
        "300"  : ["Rome", "Paris", "Atlanta"],
        "400a" : ["Los Angeles"]
    }
    

    It is very easy of course: you either use Map<String, String> as an input (which I believe you already do), or you can pass in Map<object, String> if you want to allow yourself to put in both string and integer values. However, they will still be serialized only as strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C# I have a use case where I have a mapping from int
I have a situation where I want to create a mapping from a tuple
I used the mapping solution from this question to have a joined component. But
I have a Map<Something, List<String>> map . I want to remove a String from
I have a table (dictionary of dictionaries) to store mapping of integers to strings.
Users of my application have the possibility of choosing some values from list. The
so I have found a few questions on SO (for example: Automapper mapping list
I have this mapping ( it comes from adventureworks since I used it in
I have a mapping application that needs to draw a path, and then display
I would like to have a mapping which maps two string into one string.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.