java中的JSON-php和gson

java中的JSON-php和gson,java,php,json,gson,Java,Php,Json,Gson,是否可以在PHP中使用json_encode()函数对类的对象变量进行编码? 如果是,那么如何使用java中的gson返回类对象字段: 条目jsonElement jsonElement.getValue.getAs。。。 可用函数getAsString、getAsInt。。etc在这种情况下没有用处。根据php.net,是的,您可以对除资源之外的任何内容进行json_编码,因此可以对类的实例进行编码 关于java;我不太熟悉,但你可以看看这里: (底部有一个示例,说明如何通过GSON获取对象。

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

条目jsonElement

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

根据php.net,是的,您可以对除资源之外的任何内容进行json_编码,因此可以对类的实例进行编码

关于java;我不太熟悉,但你可以看看这里:


(底部有一个示例,说明如何通过GSON获取对象。根据php.net,是的,您可以对除资源之外的任何内容进行json编码,因此可以对类的实例进行编码

关于java,我不太熟悉,但您可以看看这里:

(底部有一个示例,说明如何通过GSON从以下位置获取对象:

JSON(JavaScript对象表示法)是一种轻量级数据交换 格式。这对人类来说很容易读和写。对人类来说很容易 要解析和生成的机器。它基于 JavaScript编程语言,标准ECMA-262第3版- 1999年12月

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

代码: 这个json_字符串可以直接导入到您喜欢的对象中

来自Java 输出 发件人:

JSON(JavaScript对象表示法)是一种轻量级数据交换 格式。这对人类来说很容易读和写。对人类来说很容易 要解析和生成的机器。它基于 JavaScript编程语言,标准ECMA-262第3版- 1999年12月

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

代码: 这个json_字符串可以直接导入到您喜欢的对象中

来自Java 输出
但是当php类用java解码时会发生什么…?这就是我要问的…但是当php类用java解码时会发生什么…?这就是我要问的。。。。
<?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
)
/*
 * 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)