java - java中的JSON php和gson

标签 java php json gson

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

条目 jsonElement.;

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

最佳答案

来自 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

它是一种数据交换格式,因此任何语言都可以使用它。这就是为什么您可以通过您喜欢的任何语言使用 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)

关于java - java中的JSON php和gson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6719084/

相关文章:

javascript - 自动从其他网站检索视频缩略图

php - 在 MySQL 中将一行的列连接为单个输出

c# - System.Text.Json 中 Newtonsoft.Json DefaultValueHandling = DefaultValueHandling.Ignore 选项的等价物是什么

java - 使用 HttpUrlConnection 时如何从 CookieManager 设置 cookie?

php - 直接从一个站点检索信息还是通过我的 web 服务器使用 php 检索信息更可取?

json - 如何与 Telegram API 交互

ios - Google Places API 未返回结果

java - TLSv1 握手失败

java - 如何使用 Spring Boot 在 API Get 方法 Endpoint 中传递多个 id

java - JMockit:测试覆盖抽象方法的方法