【问题标题】:Converting JSON into java parse将 JSON 转换为 java 解析
【发布时间】:2013-02-11 11:57:25
【问题描述】:

我正在我的服务器上执行一个查询,它以 JSON 格式返回树信息。下面

[{"leaf": 0, "context": {}, "text": "ABC-1-6-1", "expandable": 1, "id": "1.1.2.202.ABC-1-6-1", "allowChildren": 1}]

我正在玩 json-simple 库,如果能以字符串格式获取上述信息,则可以使用 JSONParser 解析它,即

String jsonString = "{\"leaf\": 0, \"context\": {}, \"text\": \"1.1.2.202.ABC-1-6-1\", \"expandable\": 1, \"id\": \"1.1.2.202.ABC-1-6-1\", \"allowChildren\": 1}";

任何帮助都会很棒!

【问题讨论】:

  • 问题是什么?
  • 你的问题是什么?
  • 我想把JSON数据解析成java
  • @daverocks 这是一个 json 数组,你能给我完整的字符串,包括名字吗?
  • 意思是要将json转成java对象?

标签: java json


【解决方案1】:

如果您想将 JSON 转换为 java,请使用 gson。很简单。

  Person fromJson = new Gson().fromJson(json, Person.class);// this will convert json to java object
        fromJson.setAge(15);
        fromJson.setName("json");
        fromJson.setEmail("email@gmail.com");
        fromJson.setMob("4657576876");

        json = new Gson().toJson(fromJson);// this will convert java to json string like this {"name":"json","age":15,"email":"email@gmail.com","mob":"4657576876"}

https://sites.google.com/site/gson/gson-user-guide

json数组转换参考以下链接

serialize and deserialize json array

【讨论】:

    【解决方案2】:

    你有很多JSON parsers for Java

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 2019-08-17
      相关资源
      最近更新 更多