简体   繁体   English

我如何从Java中的JSON字典中获取信息

[英]How can i get information from JSON dictionary in java

I have a dictionary on a website in form of json which looks for example like this: 我在网站上有json形式的字典,例如:

"types":[{
    "id":"0",
    "name":"value1"
    },{
    "id":"1",
    "name":"value2"
    },{
    "id":"2",
    "name":"value3"
    }]

I cant find any useful example of a method that could help me retrieve that information and put it in, for example, a string array. 我找不到任何有用的方法示例,可以帮助我检索该信息并将其放入例如字符串数组中。 Maybe anyone of you come across same problem please help! 也许你们中有人遇到相同的问题,请帮忙!

PS I tried method like simplest way to read json from a URL in java with no success. PS我尝试了最简单的方法来从Java中的URL读取json ,但没有成功。

It is array of json object value whose key is types . 它是键为types的json对象值的数组。 You can use json-java library like gson, jackson,flexjson etc.. to parse this json object and achieve your required result. 您可以使用gson, jackson,flexjson etc.. json-java库解析此json对象并获得所需的结果。

GSON Example - GSON示例-

JsonElement jElement = new JsonParser().parse(jsonString);
JsonObject  jObject = jElement.getAsJsonObject();
jObject = jObject.getAsJsonObject("types");

Or you can use pojo class like - 或者您可以使用类似的pojo类-

public class Type {
  private int id;
  private String name;
  ...
}

gson.fromJson(jsonString, Type.class);

Refer java docs . 请参阅java docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM