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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:37:33+00:00 2026-06-13T10:37:33+00:00

import java.util.HashMap; public class JSON { public String name; public HashMap<String, String> Credentials =

  • 0
import java.util.HashMap;

public class JSON {

    public String name;

    public HashMap<String, String> Credentials = new HashMap<String, String>();

    public JSON(String name){
        Credentials.put(name, name);
    }

}

JSON json = new JSON("Key1");
new Gson().toJson(json);

I get the following value as output.

{“Credentials”:{“Key1″:”Key1”}}

Now how would i create an JSONObject something like this below using Gson.

  • 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-13T10:37:34+00:00Added an answer on June 13, 2026 at 10:37 am

    Building on what @Brian is doing, you just need the auto serialization piece.

    What you do is the following, and I have to state, this is with regard to a single object at the moment. You’ll have to look through the GSON documentation for more detail on this if you’re dealing with a collection of objects at the top level.

    Gson gson= new Gson();
    Writer output= ... /// wherever you're putting information out to
    JsonWriter jsonWriter= new JsonWriter(output);
    // jsonWriter.setIndent("\t"); // uncomment this if you want pretty output
    // jsonWriter.setSerializeNulls(false); // uncomment this if you want null properties to be emitted
    gson.toJson(myObjectInstance, MyObject.class, jsonWriter);
    jsonWriter.flush();
    jsonWriter.close();
    

    Hopefully that will give you enough context to work with. Gson should be smart enough to figure out your properties and give them sensible names in the output.

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

Sidebar

Related Questions

(ns lol.test (:gen-class :name lol.test :methods [[createHashMap [String] Java.util.HashMap]]) (:import [java.util HashMap])) (defn -createHashMap
import java.util.Scanner; public class Main extends Hashmap{ public static void main(String[] args) { Hashmap
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) {
import java.util.Scanner; public class CourseSplitter { public static void main(String args[]){ Scanner keyboard =
import java.util.*; import java.io.*; public String recToString (boolean format) { Date date = new
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]
import java.io.*; import java.util.*; public class Readfilm { public static void main(String[] args) throws
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class InversionCounter { public static void main(String[]
import java.lang.ref.SoftReference; import java.util.HashMap; import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Message; public class AsyncImageLoader {

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.