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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:24:00+00:00 2026-06-03T23:24:00+00:00

import org.json.simple.JSONArray; import org.json.simple.JSONAware; import org.json.simple.JSONObject; import org.json.simple.JSONValue; public class JsonTest implements JSONAware {

  • 0
import org.json.simple.JSONArray;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class JsonTest implements JSONAware {
private final int x, y;

public JsonTest(int x, int y) {
    this.x = x;
    this.y = y;
}

@Override
public String toJSONString() {
    JSONArray arr = new JSONArray();
    arr.add(this.x);
    arr.add(this.y);
    return arr.toString();
}

public static void main(String[] args) {
    JsonTest jtest = new JsonTest(4, 5);
    String test1 = JSONValue.toJSONString(jtest);
    System.out.println(test1); //this works as expected
    JSONObject obj = new JSONObject();
    obj.put(jtest, "42");
    System.out.println(obj); //this doesn't
}
}

Gives as output:

[4,5]

{"it.integrasistemi.scegliInPianta.etc.JsonTest@3cb89838":"42"}

Instead of:

[4,5]

{[4,5]:"42"}

What am i missing?

My reference: http://code.google.com/p/json-simple/wiki/EncodingExamples#Example_6-1_-_Customize_JSON_outputs

  • 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-03T23:24:02+00:00Added an answer on June 3, 2026 at 11:24 pm

    That’s because JSonTest doesn’t override the toString() method.

    Add the following code to the JSonTest class:

    @Override
    public String toString() {
        return toJSONString(); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following Code package cyclist.project; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException;
import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapPrimitive; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.AndroidHttpTransport; import org.ksoap2.transport.HttpTransportSE; public class
The following is my Class code import java.net.*; import java.util.*; import java.sql.*; import org.apache.log4j.*;
below is a sample class: AAAA.java 1 package tp.domain; 2 3 import org.springframework.beans.factory.annotation.Value; 4
package com.crumbin.tabs; //java package import java.io.IOException; import java.util.ArrayList; import org.json.JSONException; import android.app.Activity; import android.app.AlertDialog;
I'm having trouble converting objects into JSONObject (org.json.JSONObject) in java. I have an object
Here is my code import org.ksoap2.*; import org.ksoap2.serialization.*; import org.ksoap2.transport.*; import android.app.Activity; import android.os.Bundle;
I have the following in a java file (MyRtmpClient.java): import org.apache.mina.common.ByteBuffer; and ByteBuffer is
I used this code in my Android application: import org.apache.http.client.methods.HttpPost; ... HttpClient httpclient =
I want to use these two classes from lucene - import org.apache.lucene.analysis.snowball.*; import org.apache.lucene.analysis.PorterStemmer;

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.