com.google.gson.Gson.toJson()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(6.4k)|赞(0)|评价(0)|浏览(220)

本文整理了Java中com.google.gson.Gson.toJson()方法的一些代码示例,展示了Gson.toJson()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Gson.toJson()方法的具体详情如下:
包路径:com.google.gson.Gson
类名称:Gson
方法名:toJson

Gson.toJson介绍

[英]Converts a tree of JsonElements into its equivalent JSON representation.
[中]将JsonElements树转换为其等效的JSON表示形式。

代码示例

代码示例来源:origin: stackoverflow.com

Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(myObject); // myObject - instance of MyObject
prefsEditor.putString("MyObject", json);
prefsEditor.commit();

代码示例来源:origin: apache/storm

public String toJson() {
  String json = gson.toJson(this);
  System.out.println(json);   // TODO log
  return json;
}

代码示例来源:origin: stackoverflow.com

Gson g = new Gson();

Person person = g.fromJson("{\"name\": \"John\"}", Person.class);
System.out.println(person.name); //John

System.out.println(g.toJson(person)); // {"name":"John"}

代码示例来源:origin: stackoverflow.com

Map<String, String> myMap = new HashMap<String, String>();
myMap.put("one", "hello");
myMap.put("two", "world");

Gson gson = new GsonBuilder().create();
String json = gson.toJson(myMap);

System.out.println(json);

Type typeOfHashMap = new TypeToken<Map<String, String>>() { }.getType();
Map<String, String> newMap = gson.fromJson(json, typeOfHashMap); // This type must match TypeToken
System.out.println(newMap.get("one"));
System.out.println(newMap.get("two"));

代码示例来源:origin: Vedenin/useful-java-links

/**
   * Example to writeJson using TreeModel
   */
  private static void writeJson() throws IOException {
    JsonObject rootObject = new JsonObject();
    rootObject.addProperty("message", "Hi");
    JsonObject childObject = new JsonObject();
    childObject.addProperty("name", "World!");
    rootObject.add("place", childObject);

    Gson gson = new Gson();
    String json = gson.toJson(rootObject);
    System.out.println(json); // print "{"message":"Hi","place":{"name":"World!"}}"
  }
}

代码示例来源:origin: stackoverflow.com

Gson gson = new GsonBuilder()
   .setExclusionStrategies(new TestExclStrat())
   //.serializeNulls() <-- uncomment to serialize NULL fields as well
   .create();
 Student src = new Student();
 String json = gson.toJson(src);
 System.out.println(json);

代码示例来源:origin: stackoverflow.com

//from object to JSON 
Gson gson = new Gson();
gson.toJson(yourObject);

// from JSON to object 
yourObject o = gson.fromJson(JSONString,yourObject.class);

代码示例来源:origin: ctripcorp/apollo

private String mockLongPollBody(String notificationsStr) {
 List<ApolloConfigNotification> oldNotifications = gson.fromJson(notificationsStr, notificationType);
 List<ApolloConfigNotification> newNotifications = new ArrayList<>();
 for (ApolloConfigNotification notification : oldNotifications) {
  newNotifications
    .add(new ApolloConfigNotification(notification.getNamespaceName(), notification.getNotificationId() + 1));
 }
 return gson.toJson(newNotifications);
}

代码示例来源:origin: gocd/gocd

@Override
public String requestMessageForNotifyPluginSettingsChange(Map<String, String> pluginSettings) {
  return new GsonBuilder().create().toJson(pluginSettings);
}

代码示例来源:origin: stackoverflow.com

Gson gson = Converters.registerDateTime(new GsonBuilder()).create();
SomeContainerObject original = new SomeContainerObject(new DateTime());

String json = gson.toJson(original);
SomeContainerObject reconstituted = gson.fromJson(json, SomeContainerObject.class);

代码示例来源:origin: stackoverflow.com

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(uglyJSONString);
String prettyJsonString = gson.toJson(je);

代码示例来源:origin: stackoverflow.com

String jsonVal0 = "{\"id\": 5382, \"user\": \"Mary\" }";
 String jsonVal1 = "{\"id\": 2341, \"person\": \"Bob\"}";
 final GsonBuilder gsonBuilder = new GsonBuilder();
 gsonBuilder.registerTypeAdapter(MyClass.class, new MyClassTypeAdapter());
 final Gson gson = gsonBuilder.create();
 MyClass myClassInstance0 = gson.fromJson(jsonVal0, MyClass.class);
 MyClass myClassInstance1 = gson.fromJson(jsonVal1, MyClass.class);
 System.out.println("jsonVal0 :" + gson.toJson(myClassInstance0));
 // output: jsonVal0 :{"id":5382,"name":"Mary"}
 System.out.println("jsonVal1 :" + gson.toJson(myClassInstance1));
 // output: jsonVal1 :{"id":2341,"name":"Bob"}

代码示例来源:origin: stackoverflow.com

import java.io.FileReader;
import java.util.ArrayList;

import com.google.gson.Gson;

public class Foo
{
 public static void main(String[] args) throws Exception
 {
  Gson gson = new Gson();
  TypeDTO[] myTypes = gson.fromJson(new FileReader("input.json"), TypeDTO[].class);
  System.out.println(gson.toJson(myTypes));
 }
}

class TypeDTO
{
 int id;
 String name;
 ArrayList<ItemDTO> items;
}

class ItemDTO
{
 int id;
 String name;
 Boolean valid;
}

代码示例来源:origin: stackoverflow.com

Gson gson = new Gson();

String inputString= gson.toJson(inputArray);

System.out.println("inputString= " + inputString);

代码示例来源:origin: stackoverflow.com

Gson gson = new GsonBuilder()
   .setExclusionStrategies(new TestExclStrat("in.naishe.test.Country.name"))
   //.serializeNulls()
   .create();
 Student src = new Student();
 String json = gson.toJson(src);
 System.out.println(json);

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) {
   ObixBaseObj lobbyObj = new ObixBaseObj();
   lobbyObj.setIs("obix:Lobby");
   ObixOp batchOp = new ObixOp();
   batchOp.setName("batch");
   batchOp.setIn("obix:BatchIn");
   batchOp.setOut("obix:BatchOut");
   lobbyObj.addChild(batchOp);
   Gson gson = GsonUtils.getGson();
   System.out.println(gson.toJson(lobbyObj));
 }

代码示例来源:origin: stackoverflow.com

Type listType = new TypeToken<LinkedList>() {}.getType();
List target = new LinkedList();
target.add("blah");

Gson gson = new Gson();
String json = gson.toJson(target, listType);
List target2 = gson.fromJson(json, listType);

代码示例来源:origin: gocd/gocd

@Test
public void shouldHandlePolymorphismWhenDeserializingAntTask()
{
  CRTask value = antTask;
  String json = gson.toJson(value);
  CRBuildTask deserializedValue = (CRBuildTask)gson.fromJson(json,CRTask.class);
  assertThat("Deserialized value should equal to value before serialization",
      deserializedValue,is(value));
}
@Test

代码示例来源:origin: alibaba/jstorm

public static String toPrettyJsonString(Object obj) {
  Gson gson2 = new GsonBuilder().setPrettyPrinting().create();
  return gson2.toJson(obj);
}

代码示例来源:origin: stackoverflow.com

Editor prefsEditor = mPrefs.edit();
  Gson gson = new Gson();
  String json = gson.toJson(MyObject);
  prefsEditor.putString("MyObject", json);
  prefsEditor.commit();

相关文章