数据处理

weclud 2018-09-12 01:52:41
有如下三个字符串数组,其长度不是固定的,但三个数组长度相同
String[] dict = {"type", "type", "type", "sex", "sex"};
String[] dictName = {"1", "2", "3", "1", "2"};
String[] dictValue = {"身份证", "户口本", "护照", "男", "女"};
请问怎么将数据处理成以下json格式
{"sex":{"1":"男","2":"女"},"type":{"1":"身份证","2":"户口本","3":"护照"}}
...全文
137 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
weclud 2018-09-12
  • 打赏
  • 举报
回复
非常感谢!!!!!!!!
qq_39936465 2018-09-12
  • 打赏
  • 举报
回复
抛砖引玉吧 public class Resource { private String dict; private String dictName; private String dictValue; public String toString() { return "\""+dictName + "\":\"" + dictValue+"\""; } public String getDict() { return "\""+dict+"\""; } public void setDict(String dict) { this.dict = dict; } public String getDictName() { return dictName; } public void setDictName(String dictName) { this.dictName = dictName; } public String getDictValue() { return dictValue; } public void setDictValue(String dictValue) { this.dictValue = dictValue; } public Resource(String dict, String dictName, String dictValue) { super(); this.dict = dict; this.dictName = dictName; this.dictValue = dictValue; } } import java.util.ArrayList; import java.util.List; public class Json { static String str; public static void main(String[] args) { String[] dict = { "type", "type", "type", "sex", "sex" }; String[] dictName = { "1", "2", "3", "1", "2" }; String[] dictValue = { "身份证", "户口本", "护照", "男", "女" }; Resource res; List<Resource> lt = new ArrayList<Resource>(); int a = 0, b = 0; // TODO Auto-generated method stub for (int i = 0; i < dict.length; i++) { res=new Resource(dict[i],dictName[i],dictValue[i]); lt.add(res); } for (Resource i : lt) { if (a == 0) { str = "{"+i.getDict() + ":{" + i.toString(); a++; } else if (i.getDict().equals("\"type\"")) { str += "," + i.toString(); } else if (b == 0) { str += "}," + i.getDict() + ":{" + i.toString(); b++; } else { str += "," + i.toString(); } } str += "}}"; System.out.println(str); } }

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧