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

  • Home
  • SEARCH
  • 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 6162239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:37:09+00:00 2026-05-23T21:37:09+00:00

Is it possible to encode using json_encode() function in PHP a variable that is

  • 0

Is it possible to encode using json_encode() function in PHP a variable that is an object of class?
If yes then how can i get back the class object fields using gson in java:

Entry jsonElement.;

jsonElement.getValue.getAs…
The available functions getAsString,getAsInt.. etc are not useful in this case.

  • 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-23T21:37:10+00:00Added an answer on May 23, 2026 at 9:37 pm

    from http://www.json.org:

    JSON (JavaScript Object Notation) is a lightweight data-interchange
    format. It is easy for humans to read and write. It is easy for
    machines to parse and generate. It is based on a subset of the
    JavaScript Programming Language, Standard ECMA-262 3rd Edition –
    December 1999

    It is a data interchange format so it can be used by any language. That’s why for example you can use Twitter’s REST api from any language you like.

    code:

    <?php
    
    class Point {
        private $x;
        private $y;
    
        public function __construct($x, $y) {
            $this->x = $x;
            $this->y = $y;
        }
    
        public static function fromJSON($json) {
            //return json_decode($json);
            $obj = json_decode($json);
            return new Point($obj->x, $obj->y);
        }
    
        public function toJSON() {
            /*
    
            If you want to omit properties because of security, I think you will have to write this yourself.
    
            return json_encode(array(
                "x" => $this->x,
                "y" => $this->y
            ));
    
            You could easily do something like to omit x for example.
    
            $that = $this;
            unset($that->x);
            return json_encode(get_object_vars($that));
    
            */
            // Thank you http://stackoverflow.com/questions/4697656/using-json-encode-on-objects-in-php/4697749#4697749
            return json_encode(get_object_vars($this));
        }
    
        public function  __toString() {
            return print_r($this, true);
        }
    }
    
    $point1 = new Point(4,8);
    
    $json = $point1->toJSON();
    echo $json;
    echo $point1;
    
    $point2 = Point::fromJSON($json);
    echo $point2;
    

    output:

    alfred@alfred-laptop:~/www/stackoverflow/6719084$ php class.php 
    {"x":4,"y":8}Point Object
    (
        [x:Point:private] => 4
        [y:Point:private] => 8
    )
    Point Object
    (
        [x:Point:private] => 4
        [y:Point:private] => 8
    )
    

    This json_string you can just import into the object you like.

    From Java

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package point;
    
    import com.google.gson.Gson;
    
    
    /**
     *
     * @author alfred
     */
    public class Point {
    
        private int x,y;
        public static Gson gson = new Gson();
    
        public Point(int x, int y) {
                this.x = x;
                this.y = y;
        }
    
        public static Point fromJSON(String json) {
            Point p = gson.fromJson(json, Point.class);
            return p;
        }
    
        @Override
        public String toString() {
            return "(" + x + "," + y + ")";
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            Point fromJSON = Point.fromJSON("{\"x\":4,\"y\":8}");
            System.out.println(fromJSON);
        }
    }
    

    Output

    (4,8)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to output specific JSON data (exported from Firefox bookmarks) using PHP.
I am using PHP json_encode() to generate JSON data of a shopping cart: {
I'm using PHP to json encode a massive multi-dimensional array of events, so i
I want my function to take an argument that could be an unicode object
Is it possible to encode an assignment into an expression tree?
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
I'm working on a simple voting system, using php and sqlite. I prebuild a
Possible Duplicate: Converting XML to JSON using Python? I am importing an XML feed
Possible Duplicate: Do I really need to encode '&' as ' &amp;' ? I

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.