【问题标题】:create and set an object创建和设置对象
【发布时间】:2021-05-11 18:37:57
【问题描述】:

我想将我的数据集值放入一个对象中。
我是新手。我想创建一个对象并将这些值放入其中,如下所示。 我该怎么做???

对象:

{
   dataType: _____,     //dataSet.getDataType().getName()
   Value: ______,      //dp.getStartTime(TimeUnit.MILLISECONDS))
   startTime: ____,    //dp.getEndTime(TimeUnit.MILLISECONDS))
   endTime: _____     //dp.getValue(field).toString()
}

private static void dumpDataSet(DataSet dataSet) {
                Log.v(TAG, "Name: " + dataSet.getDataType().getName());
                DateFormat dateFormat = DateFormat.getTimeInstance();
                Log.v(TAG, "Fields: " + dataSet.getDataSource().getDataType().getFields());

                Log.v(TAG, "Data Point Values :" + dataSet.getDataPoints());
                for (DataPoint dp : dataSet.getDataPoints()) {
                    Log.v(TAG, "Data Point:");
                    Log.v(TAG, "Type: " + dataSet.getDataType().getName());
                    Log.v(TAG, "Start: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
                    Log.v(TAG, "End: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)));
                    for (Field field : dp.getDataType().getFields()) {
                        Log.v(TAG, "Field: " + field.getName() + ", Value : " + dp.getValue(field).toString());
                        
                    }
                }
            }

【问题讨论】:

  • 你是想把它转换成json对象还是有其他类?
  • 试图转换为 json 对象

标签: java android class object


【解决方案1】:

使用 Gson

implementation 'com.google.code.gson:gson:2.8.6'

喜欢这个

Gson gson = new GsonBuilder().create();
JSONObject jsonObject = new JSONObject(gson.toJson(dp));

【讨论】:

    【解决方案2】:

    您可以简单地使用 Jackson API ObjectMapper 类将 Java 对象转换为 jsonString。

    首先添加这些依赖项:

    implementation  'com.fasterxml.jackson.core:jackson-core:2.6.3'
    implementation  'com.fasterxml.jackson.core:jackson-annotations:2.6.3'
    implementation  'com.fasterxml.jackson.core:jackson-databind:2.6.3'
    

    到您的 build.gradle(应用程序级别)文件。 之后:

        ObjectMapper mapper = new ObjectMapper();
    try {
      String json = mapper.writeValueAsString(dataSet);
      System.out.println("ResultingJSONstring = " + json);
    
    } catch (JsonProcessingException e) {
       e.printStackTrace();
    }
    

    如果您需要将其转换为 Json 对象,则:

     JSONObject jsonObject = new JSONObject(json);
    

    【讨论】:

      【解决方案3】:

      您可以直接使用JSONObject 并将value 与关联的key 添加到其中

       JSONObject jsonObject = new JSONObject();
       jsonObject.put("dataType", dataSet.getDataType().getName());
       jsonObject.put("Value", dp.getStartTime(TimeUnit.MILLISECONDS)));
       jsonObject.put("startTime", dp.getEndTime(TimeUnit.MILLISECONDS)));
       jsonObject.put("endTime", dp.getValue(field).toString());
      

      使用jsonObject.toString()可以得到String格式的Json

      【讨论】:

      • 这将创建一个本地 jsonObject 权限。我可以在另一个类中调用这个 JSONobject 吗??
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多