【问题标题】:JSON php and gson in javajava中的JSON php和gson
【发布时间】:2011-10-06 20:08:56
【问题描述】:

是否可以在 PHP 中使用 json_encode() 函数对作为类对象的变量进行编码? 如果是,那么我如何在 java 中使用 gson 取回类对象 fields

输入 jsonElement.;

jsonElement.getValue.getAs... 在这种情况下,可用的函数 getAsString、getAsInt.. 等没有用。

【问题讨论】:

    标签: java php json gson


    【解决方案1】:

    根据 php.net,是的,您可以对资源以外的任何内容进行 json_encode,因此可以对类的实例进行编码。 http://php.net/manual/en/function.json-encode.php

    关于java;我不太熟悉,但你看这里:How to decode a json string with gson in java?

    (底部有一个示例如何通过GSON获取对象

    【讨论】:

    • 但是当php类在java中解码时会发生什么...?这就是我要问的......
    【解决方案2】:

    来自http://www.json.org

    JSON(JavaScript Object Notation)是一种轻量级的数据交换 格式。人类很容易阅读和写作。这很容易 机器来解析和生成。它基于一个子集 JavaScript 编程语言,标准 ECMA-262 第 3 版 - 1999 年 12 月

    它是一种数据交换格式,因此可以被任何语言使用。这就是为什么你可以使用任何你喜欢的语言的 Twitter 的 REST api。

    代码:

    <?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;
    

    输出:

    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
    )
    

    这个json_string你可以直接导入到你喜欢的对象中。

    来自 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);
        }
    }
    

    输出

    (4,8)
    

    【讨论】:

      猜你喜欢
      • 2020-09-17
      • 2013-11-26
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多