如何在 Java 对象中转换字符串数组 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23851132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:33:19  来源:igfitidea点击:

How to Convert String Array JSON in a Java Object

javajsonunmarshalling

提问by Deividson Calixto

i need to convert this String JSON to a Java Object:

我需要将此字符串 JSON 转换为 Java 对象:

{"estabelecimento":[{"id":"5","idUsuario":"5","razaoSocial":"Bibi LTDA","nomeFantasia":"BibiPizza","telefone":"22121212","email":"[email protected]","gostaram":"0"},{"id":"8","idUsuario":"1","razaoSocial":"Nestor Latuf LTDA","nomeFantasia":"Nestor Sorvetes","telefone":"32343233","email":"[email protected]","foto":"","gostaram":"0"},{"id":"9","idUsuario":"1","razaoSocial":"Comercio Alimenticio Rivaldo","nomeFantasia":"Rogers Burguer","telefone":"210021020","email":"[email protected]","foto":"","gostaram":"0"}]}

I try this, but not work:

我试试这个,但不起作用:

 //JSONArray jArr = new JSONArray(br.toString());  

        //JSONObject jObj = new JSONObject(br.toString());

        //JSONArray jArr = jObj.getJSONArray("list");

        JSONArray jArr = new JSONArray(br.toString());

        for (int i=0; i < jArr.length(); i++) {
            JSONObject obj = jArr.getJSONObject(i);
            estabelecimento.setId(obj.getLong("id"));
            estabelecimento.setIdUsuario(obj.getLong("idUsuario"));
            estabelecimento.setRazaoSocial(obj.getString("razaoSocial"));
            estabelecimento.setNomeFantasia(obj.getString("nomeFantasia"));
            estabelecimento.setTelefone(obj.getString("telefone"));
            estabelecimento.setEmail(obj.getString("email"));
            estabelecimento.setGostaram(obj.getInt("gostaram"));

            estabelecimentoList.add(estabelecimento);
        }
        con.disconnect();

How can i obtain a Java Object? Someone can help? tks.

我如何获得 Java 对象?有人可以帮忙吗?tks。

采纳答案by Marco Di Scala

You can use the Gson lib of google:

您可以使用谷歌的 Gson 库:

public class MyClass {

    private int data1 = 100;
    private String data2 = "hello";
    private List<String> list = new ArrayList<String>() {
      {
        add("String 1");
        add("String 2");
        add("String 3");
      }
    };

    //getter and setter methods needed


}


String str = {"data1":100,"data2":"hello","list":["String 1","String 2","String 3"]};
com.google.gson.Gson gson = new com.google.gson.Gson();

//To convert json string to class use fromJson
MyClass obj = gson.fromJson(str, MyClass .class);

//To convert class object to json string use toJson
String json = gson.toJson(obj);

回答by Oleg Gryb

At high level two major steps:

在高层有两个主要步骤:

  1. Generate a Java class from your JSON, e.g. by using this generator or similar: http://www.jsonschema2pojo.org/
  2. Use Hymanson processor to deserialize your JSON file:

     ObjectMapper mapper = new ObjectMapper();
     YourGeneratedClass obj = (YourGeneratedClass) mapper.readValue(new File("path-to-your-json-file"), YourGeneratedClass.class);
    
  1. 从你的 JSON 生成一个 Java 类,例如使用这个生成器或类似的:http: //www.jsonschema2pojo.org/
  2. 使用 Hymanson 处理器反序列化您的 JSON 文件:

     ObjectMapper mapper = new ObjectMapper();
     YourGeneratedClass obj = (YourGeneratedClass) mapper.readValue(new File("path-to-your-json-file"), YourGeneratedClass.class);
    

More about Hymanson: http://Hymanson.codehaus.org/

关于Hyman逊的更多信息:http: //Hymanson.codehaus.org/

You can also create YourGeneratedClass manually if you feel comfortable enough with this.

如果您对此感到满意,也可以手动创建 YourGeneratedClass。