简体   繁体   English

使用Gson库将Java String对象转换为Json

[英]Converting Java String object into Json using Gson library

I am able to convert the complex Java objects like List , ArrayList etc containing huge volume of data into Json efficiently using the Gson library as below code 我可以使用下面的代码使用Gson库将包含大量数据的复杂Java对象(如ListArrayList等)有效地转换为Json

    List<CusPojo> list=new ArrayList<CusPojo>();
       .
       .
     Gson gson=new Gson();
     String json=gson.toJson(list);

But if if try the same for a String literal or a String Obj , the conversion is not happening 但是,如果对String文字String Obj尝试相同的操作,则转换不会发生

    String msg="success";
            **or**
    String msg=new String("success");

    Gson gson=new Gson();
    String json=gson.toJson(msg); 
    System.out.println("json data-- "+json);

Here i expect the data in Json format like 在这里我期望像Json格式的数据

    json data-- {"msg":"success"}

but instead success is what i am getting 但是成功却是我得到的

    json data-- "success"

I couldn't find any explanation regarding this particulary Please help , thank you in advance.. 我找不到关于此特殊情况的任何解释。请帮助,在此先谢谢您。

Please note, I rarely write java anymore, and I don't have a compiler in front of me, so there could be some obvious bugs here. 请注意,我很少再编写Java了,而且我前面没有编译器,因此这里可能存在一些明显的错误。 But try this. 但是尝试一下。

Assuming you have a class for example: 假设您有一个课程,例如:

public class Test {
    public String msg;
}

You can use it, rather than a string in your gson example. 您可以使用它,而不是gson示例中的字符串。

public static void main(String args[]) {
    Test test = new Test();
    test.msg = "success"
    Gson gson = new Gson();
    String json = gson.toJson(test);
}

For simple cases where you don't want to use a POJO or map you can just create a JsonObject where it's behaviour is close to a map so you can pass the string as value and provide a property name where the JsonObject's toString() will be in JSON format. 对于不想使用POJO或地图的简单情况,您可以在行为接近地图的地方创建一个JsonObject ,以便可以将字符串作为值传递,并提供一个属性名称,其中JsonObject的toString()将用作JSON格式。 So you could do the following: 因此,您可以执行以下操作:

JsonObject jObj = new JsonObject();
jObj.addProperty("msg", msg);
System.out.println(jObj);

GSon doesn't save you variable name, it saves field name of serialized class GSon不会保存您的变量名,而是保存序列化类的字段名

public class StringSerialize{
      private String msg;
      ......
}

I think you are doing it wrong. 我认为您做错了。 your variable msg is just holding the data. 您的变量味精只是保存数据。 try this, let me know if it helps 试试这个,让我知道是否有帮助

    Map<String,String> map = new HashMap<>();       
    map.put("msg", "success");
    Gson gson = new GsonBuilder().serializeNulls().create();
    String json = gson.toJson(map);
    System.out.println(json);
Map<String, String> m = new HashMap<String, String>();
m.put("msg", "success");
Gson gson = new GsonBuilder().create();
System.out.println(gson.toJson(m));

Result 结果

{"msg":"success"}

Try this for your example: 尝试以下示例:
At first create a model class 首先创建一个模型类

class Model {
    public String msg;
}

The name of your member is the key in your json 成员的名称是json中的键

public static void main() {
    Model model = new Model();
    model.msg = "success"
    Gson gson = new Gson();

    String json = gson.toJson(test); // Model to json
    Model result = gson.fromJson("{\"msg\":\"success\"}", Model.class) // json to Model
}

This is a very good gson-tutorial linked by the gson repository on github 这是一个很好的gson教程,由github上的gson存储库链接

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

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