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 7991817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:23:58+00:00 2026-06-04T13:23:58+00:00

in my java code, I have a class with o member of type Map<String,String>

  • 0

in my java code, I have a class with o member of type Map<String,String>. I use google Gson library to convert from object to json string and viceversa.

I want to do the same with javascript and the JSON library from json.org.

The problem is javascript hasn’t “map objects” and if I use arrays to emulate maps, the results of JSON.stringify over arrays is not the same as Gson over Map<String,String>.

I also tried with Hashtable javascript object from the library jshashtable 2.1 (http://www.timdown.co.uk/jshashtable/index.html) and creating a custom toJSON method but this approach didn’t work because what JSON does in these cases is stringify the object returned by the custom toJSON method.

This is what I get:

With java and Gson:

{
    "var_1": "value_1",
    "var_2": "value_2",
    "map": {
        "name_1": "value_1",
        "name_2": "value_2"
    }
}

With javascript and JSON:

{
    "var_1": "value_1",
    "var_2": "value_2",
    "map": [
        ["name_1", "value_1"],
        ["name_2", "value_2"]
    ]
}

Is there any way to resolve this and obtain the same “json strings” from java and javascript?

Thanks in advance…

  • 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-06-04T13:24:00+00:00Added an answer on June 4, 2026 at 1:24 pm

    The problem is javascript hasn’t “map objects”…

    Yes, it does: All JavaScript objects are maps. To get what you’re looking for, you’d create this object graph:

    var obj = {
        "var_1": "value_1",
        "var_2": "value_2",
        "map": {
            "name_1": "value_1",
            "name_2": "value_2"
        }
    };
    

    …and use JSON.stringify on it.

    var json = JSON.stringify(obj);
    

    That bit after var obj = looks familiar? That’s because it’s a JavaScript object initializer, and JSON is a subset of object initializer syntax. 🙂 You could build that object in other ways if you like:

    var obj = {};
    obj.var_1 = "value_1";
    obj.var_2 = "value_2";
    obj.map = {};
    obj.map.name_1 = "value_1";
    obj.map.name_2 = "value_2";
    

    …which would also give you the same thing after calling JSON.stringify on it.

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

Sidebar

Related Questions

I have a java class that contains a hash map as a member. This
I have the following Java code: public class FirstActivity extends Activity implements OnClickListener {
I have the following Java code - import java.lang.reflect.Field; public class AnnotationTest { public
Say suppose I have the following Java code. public class Example { public static
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
Have there been incompatibilities between Java releases where Java source code/Java class files targeting
Hy i have the following code : import java.sql.*; import java.net.*; public class binsz
I have the following code in an abstract java class: protected abstract <E extends
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
In my Java code I have function that gets file from the client in

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.