Convert to Json

I have a question about the toJson function in google.Gson library.

I read online here that I can use it like this for Json (_), what does the underscore mean? In the documentation, the parameter represents the target type, can you explain what should I pass if I just want to convert the log data to Json?

Here is the code I'm asking about:

  private var gson = new Gson()
 val tweetStream = TwitterUtils.createStream(ssc, Utils.getAuth)
      .map(gson.toJson(_))
+4
source share
1 answer

Hesson decided that all of Json's riots was extremely light.

Anyway, I used Gson in java on Android and looked like this:

static public <T extends JsonAble> T getJson(Context context, Class<T> jsonClass){
    String strClassName = jsonClass.getSimpleName();
    String strJson = getTextFromFile(context, strClassName);
    T objReturn = gsnEncoder.fromJson(strJson, jsonClass);
    return objReturn;
}

As you can see, you just need to select the object, and Gson will do the magic.

EDIT: I sent fromJson instead of toJson), the idea is the same, give me a second to fix this.

static public <T extends JsonAble> boolean setObject(Context context, T Obj)  {
    boolean IsSucceed = false;
    String strClassName = Obj.getClass().getSimpleName();

    try {
        String strObj =  DL.gsnEncoder.toJson(Obj);
        IsSucceed = DL.setTextToFile(context, strClassName, strObj);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return IsSucceed;
}
-1
source

All Articles