Java服务器的JSON格式 [英] JSON formatting for a Java Server

查看:80
本文介绍了Java服务器的JSON格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用gson将JSON字符串读取到Java程序中.在下面的示例代码中-Java程序具有3个对象类. json字符串中的数据将具有可变数量的每个类的对象实例.我尝试创建一个示例JSON-来解析..,但是在解析各种对象时遇到了问题.

I am trying to read JSON string using gson into a Java program. In the sample code below - the Java program has 3 object classes. The data in the json string will have a variable number of object instances of each class. I have tried to create a sample JSON - to parse .. but had problems parsing the various objects.

这是使用json字符串的正确方法还是可以以其他方式完成.您将如何解析具有不同类的变量对象的json.谢谢,

Is this the right way to consume a json string or can it be done in a different way.. How would you parse a json with variable objects of different classes. Thanks,

package newpackage;
import java.util.ArrayList;

import com.google.gson.Gson;

public class jsonsample {



public static void main(String[] args) {

    String jsonstring = "{'TableA':[{'field_A1':'A_11'},{'field_A1':'A_12'}]}"
            + ",{'TableB':[{'field_B1':'B_11','field_B2':'B_12','field_B3':['abc','def','ghi']},"
            + "{'field_B1':'B_21','field_B2':'B_Field22','field_B3':['mno','pqr','xyz']}]"
            + ",{'TableC':[{'field_C1':'C_11','field_C2':'C_12','field_C3':'C_13'},"
            + "{'field_C1':'C_21','field_C2':'C_22','field_C3':'C_23'},{'field_C1':'C_31','field_C2':'C_32','field_C3':'C_33'}]}";
    jsonstring = jsonstring.replace('\'', '"');



}

public class TableA {
    String field_A1;

    public TableA(String a){
        this.field_A1 = a;
    }

    @Override
    public String toString() {
        return ("Table A" + " " + this.field_A1);
    }

}

public class TableB {
    String field_B1;
    String field_B2;
    ArrayList<String> field_B3 = new ArrayList<String>();

    public TableB(String a, String b, ArrayList<String> c){
        this.field_B1 = a;
        this.field_B2 = b;
        this.field_B3 = c;
    }

    @Override
    public String toString() {
        return ("Table B" + " " + this.field_B1+ " " + this.field_B2);
    }

}

public class TableC {
    String field_C1;
    String field_C2;
    String field_C3;

    public TableC(String a, String b, String c){
        this.field_C1 = a;
        this.field_C2 = b;
        this.field_C3 = c;
    }

    @Override
    public String toString() {
        return ("Table C" + " " + this.field_C1 + " " + this.field_C2 + " " + this.field_C3);
    }

}

}

推荐答案

首先,您必须确定基本的json结构是什么?最大标识符,最大值,最大对象,最大数组...

First of all you have to decide what is your base json structure ? Max identifiers, max values, max objects,max arrays...

  1. 使用texteditor或 http://www.jsoneditoronline.org/创建您的完整json结构 http://jsonlint.com/

让我们认为这是我完整的json结构:

Let's think this is my full json structure:

{
  "array": [
    1,
    2,
    3
  ],
  "boolean": true,
  "null": null,
  "number": 123,
  "object": {
    "a": "b",
    "c": "d",
    "e": "f"
  },
  "string": "Hello World"
}

  1. 创建Java类,就像json标识符一样.您可以使用 http://json2csharp.com/转换为Java.
  1. Create your Java Classes as like as your json identifiers. You can use http://json2csharp.com/ convert to Java.

这些是我的Java类:

And these are my Java Classes:

public class Object
{
    public string a { get; set; }
    public string c { get; set; }
    public string e { get; set; }
}

public class RootObject
{
    public ArrayList<int> array { get; set; }
    public Boolean boolean { get; set; }
    public Object @null { get; set; }
    public int number { get; set; }
    public Object @object { get; set; }
    public string @string { get; set; }
}

  1. 创建您的DAO ,以将其转换为结构.
  1. Create your DAO for convert these to structure to them.

对于Java;

String data = "jsonString"; 
RootObject root = new GsonBuilder().create().fromJson(data, RootObject.class);

对于Json;

Gson gson = new GsonBuilder().setDateFormat("dd/MM/yyyy").create();
String json = gson.toJson(obj);

这篇关于Java服务器的JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆