将Json转换为类型脚本类[ts]

时间:2018-07-02 12:50:17

标签: java angularjs typescript

我有以下Json

{"mapData":{"test":"success","publicKey":""},"statusCode":200,"message":null}

如何将以下json转换为Type脚本类

mapData 具有匿名值

  • 可能是{"test":"success","publicKey":""}
  • 或者可能是{"test":"success","publicKey":"","anotherKey":"anotherValue"}

那么我们如何才能将此json转换为键入脚本对象?

{"mapData":{"test":"success","publicKey":""},"statusCode":200,"message":null}

我的演示示例

export class GenericResponse {

 mapData: any;
 statusCode: number;
 message: string;
}

以下是我用来将Object转换为Json的Java类

/**
  * @author atwa  
  * Jul 2, 2018
  */
 public class Response {

/**
 * An Map that contains the actual objects
 */
private Map<String, Object> mapData =new HashMap<>();

/**
 * A String containing error code. Set to 1 if there is an error
 */
private int statusCode = 0;

/**
 * A String containing error message.
 */
private String message;


/**
 * @return the mapData
 */
public Map<String, Object> getMapData() {
    return mapData;
}

/**
 * @param mapData
 *            the mapData to set
 */
public void setMapData(Map<String, Object> mapData) {
    this.mapData = mapData;
}

}

1 个答案:

答案 0 :(得分:0)

我尝试了以下方法。

Start "" /B "%CUSTOM_JAVA_HOME%\bin\javaw" %RUN_OPTS%

test.json是下面的文件

  export class GenericResponse {

 mapData: any;
 statusCode: number;
 message: string;
 }

在TS文件中

   {"mapData":{"test":"success","publicKey":""},"statusCode":200,"message":null}

Response in the object

希望这会有所帮助