dongyan5641 2011-07-16 17:38
浏览 34
已采纳

java中的JSON php和gson

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.

  • 写回答

2条回答 默认 最新

  • doujia1939 2011-07-16 20:21
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应