将一个列值转换为键,将另一个列值转换为值

时间:2018-12-19 04:54:20

标签: java spring-mvc spring-boot

从服务中获取JSON示例

{
    "appID": 1,
    "appName": "app1",
    "screenID": 1,
    "screens": "Login",
    "textID": 1,
    "keyName": "TEXT_1",
    "text_EN": "USERNAME",
    "text_DE": null,
    "text_PE": null
},
{
    "appID": 1,
    "appName": "app1",
    "screenID": 1,
    "screens": "Login",
    "textID": 2,
    "keyName": "TEXT_2",
    "text_EN": "PASSWORD",
    "text_DE": null,
    "text_PE": null
},
{
    "appID": 1,
    "appName": "app1",
    "screenID": 1,
    "screens": "Login",
    "textID": 3,
    "keyName": "TEXT_3",
    "text_EN": "Fingerprint Login",
    "text_DE": null,
    "text_PE": null
}

我想将文本文件写为

  

“ TEXT_1” =“ USERNAME”;

     

“ TEXT_2” =“密码”;

     

“ TEXT_3” =“指纹登录”;

1 个答案:

答案 0 :(得分:0)

尝试使用org.json:

String json = ""; // Your jsonArray
JSONObject result = new JSONObject();
JSONArray arr = new JSONArray(json);
for(int i = 0; i< arr.length() ; i++) {
  JSONObject jsonObj = arr.getJSONObject(i);
  result.put(jsonObj.getString("keyName"), jsonObj.getString("text_EN"));
}
System.out.println(result);

结果:

{"TEXT_1":"USERNAME","TEXT_2":"PASSWORD","TEXT_3":"Fingerprint Login"}