Java 如何从该映射数据构造JSON
我有一张带有键和值的地图,如下图所示Java 如何从该映射数据构造JSON,java,Java,我有一张带有键和值的地图,如下图所示 {Ice creams=[Cone,KoolCool(21), Stick,KoolCool(25)]} 有了这个,我需要构造一个如下的json { "name": "Ice creams", "T2": [ { "name": "Cone", "T3": [ { "name": "KoolCool",
{Ice creams=[Cone,KoolCool(21), Stick,KoolCool(25)]}
有了这个,我需要构造一个如下的json
{
"name": "Ice creams",
"T2": [
{
"name": "Cone",
"T3": [
{
"name": "KoolCool",
"leaf": []
}
]
},
{
"name": "Stick",
"T3": [
{
"name": "KoolCool",
"leaf": []
}
]
}
]
}
每个逗号分隔的值将增加T值
我正试图通过以下方式来了解这一点
package com.util;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
public class Test {
public static void main(String args[]) {
Map<String, LinkedList<String>> consilatedMapMap = new LinkedHashMap<String, LinkedList<String>>();
LinkedList<String> values = new LinkedList<String>();
values.add("Cone,KoolCool(21)");
values.add("Stick,KoolCool(25)");
consilatedMapMap.put("Ice creams", values);
/*
* for (Map.Entry<String, LinkedList<String>> consilMap :
* consilatedMapMap.entrySet()) {
*
* String t1Category = consilMap.getKey(); LinkedList<String>
* consiladatedList = consilMap.getValue();
*
*
* for(int i=0;i<consiladatedList.size();i++) { String result =
* consiladatedList.get(i);
*
* String spliter[] = result.split(",");
*
* for(int j=0;j<spliter.length;j++) { System.out.println(spliter[j]); }
*
* }
*
* }
*/
}
}
package com.util;
导入java.util.LinkedHashMap;
导入java.util.LinkedList;
导入java.util.Map;
公开课考试{
公共静态void main(字符串参数[]){
Map consistedMap=新建LinkedHashMap();
LinkedList值=新建LinkedList();
添加(“Cone,Koolcol(21)”;
添加(“棍棒,Koolcol(25)”;
consilatedMap.put(“冰淇淋”,值);
/*
*对于(Map.Entry consilMap:
*ConsistatedMap.entrySet()){
*
*字符串t1Category=consilMap.getKey();LinkedList
*consiladatedList=consilMap.getValue();
*
*
*对于(inti=0;i,Google代码中有一个名为Gson的实用程序库。您可以查看它
有关一些简单示例,请访问以下代码:
Map<String, LinkedList<String>> consilatedMapMap = new LinkedHashMap<String, LinkedList<String>>();
LinkedList<String> values = new LinkedList<String>();
values.add("Cone,KoolCool(21)");
values.add("Stick,KoolCool(25)");
consilatedMapMap.put("Ice creams", values);
Gson gson=new Gson();
String jsonElement=gson.toJson(consilatedMapMap);
System.out.println(jsonElement);
从代码及其输出可以看出,Gson库将把数据结构转换为json字符串
你所需要的只是Gson库。我相信你把这个复杂化了-自动递增的名称…JSON结构与Java结构不匹配…JSON部分由对象结构生成,部分由解析其中的字符串生成…即使你说“每个逗号分隔的值都会增加t值”从这个例子来看,这显然是不正确的
然而……这是可能的——即使只是使用org.json(不确定为什么人们一直建议使用GSON来解决这一切……)
这里的主要思想是为需要生成的每个零件创建一个方法,并在需要时传递“level”以生成适当的“Tn”属性
public class Test {
private static JSONObject processString(String data, int level) throws JSONException {
JSONObject json = new JSONObject();
int index = data.indexOf(',');
String name = data;
String remainder = "";
if (index < 0) {
index = name.indexOf('(');
if (index > 0) {
name = data.substring(0, index);
}
} else {
name = data.substring(0, index);
remainder = data.substring(name.length() + 1);
}
json.put("name", name);
JSONArray a = new JSONArray();
if (remainder.length() > 0) {
a.put(processString(remainder, level + 1));
json.put("T" + level, a);
} else {
json.put("leaf", a);
}
return json;
}
private static JSONArray processList(List<String> list, int level) throws JSONException {
JSONArray json = new JSONArray();
for (String data : list) {
json.put(processString(data, level));
}
return json;
}
private static JSONObject processMap(Map<String>, List<String>> map, int level) throws JSONException {
JSONObject json = new JSONObject();
for (String key : map.keySet()) {
json.put("name", key);
json.put("T" + level, processList(map.get(key), level + 1));
}
return json;
}
public static void main(String args[]) {
Map<String, List<String>> consilatedMapMap = new LinkedHashMap<String, List<String>>();
List<String> values = new LinkedList<String>();
values.add("Cone,KoolCool(21)");
values.add("Stick,KoolCool(25)");
consilatedMapMap.put("Ice creams", values);
try {
int level = 2;
JSONObject json = processMap(consilatedMapMap, level);
} catch(JSONException x) {
x.printStackTrace();
System.exit(-1);
}
}
公共类测试{
私有静态JSONObject processString(字符串数据,int级别)抛出JSONException{
JSONObject json=新的JSONObject();
int index=data.indexOf(',');
字符串名称=数据;
字符串余数=”;
如果(指数<0){
index=name.indexOf('(');
如果(索引>0){
name=data.substring(0,索引);
}
}否则{
name=data.substring(0,索引);
余数=data.substring(name.length()+1);
}
json.put(“name”,name);
JSONArray a=新的JSONArray();
if(余数.length()>0){
a、 put(processString(余数,级别+1));
json.put(“T”+级别,a);
}否则{
put(“叶子”,a);
}
返回json;
}
私有静态JSONArray进程列表(列表列表,int级别)抛出JSONException{
JSONArray json=新的JSONArray();
用于(字符串数据:列表){
put(processString(数据,级别));
}
返回json;
}
私有静态JSONObject processMap(映射,列表>映射,int级别)抛出JSONException{
JSONObject json=新的JSONObject();
for(字符串键:map.keySet()){
put(“name”,key);
put(“T”+级别,processList(map.get(key),级别+1));
}
返回json;
}
公共静态void main(字符串参数[]){
Map consistedMap=新建LinkedHashMap();
列表值=新建LinkedList();
添加(“Cone,Koolcol(21)”;
添加(“棍棒,Koolcol(25)”;
consilatedMap.put(“冰淇淋”,值);
试一试{
智力水平=2;
JSONObject json=processMap(ConsistedMap,级别);
}捕获(JSONException x){
x、 printStackTrace();
系统退出(-1);
}
}
您尝试过Gson吗?没有,我正在使用org.json.JSONObject进行此操作,请告诉我Gson在这种情况下有何用处??但我需要根据需要添加T2、T3---等。T2、T3等也适用于此。json字符串将相应生成。在这种情况下,您可以演示如何生成T2、T3吗?创建一个类T2添加您需要的字段uire在它们中使用setter设置字段的值并将其放入映射。如果您想要一个特定的Json层次结构,我建议您使用字段构建一个顶级类,并将其放入数组列表中,然后从中生成Json。非常感谢,如果我添加值的话。add(“Stick,Koolcol,Sai(25)”);,为什么它不显示T4?这就是“每个逗号分隔的值都会增加t值”的意思…编辑以添加一个递归processString来处理此问题…您能告诉我如何使名称显示在T3之后(因为它目前正在生成)吗这破坏了JS搜索——JSON或JavaScript都不能保证属性的枚举顺序。即使您手动按给定顺序生成JSON,也不能保证一旦它变成JavaScript对象,并且您迭代它的属性时,属性将按该顺序访问您的recursiveSearch
函数,在if(isObject(json))中
部分。您现在要做的是在找到name
属性后,迭代所有属性并设置startSaving=true
。相反,您应该检查该对象上的name
属性,并在开始循环之前检查它是否等于搜索项。我不完全确定您想要返回什么从您的示例中的搜索中,我假设它是匹配节点的所有子节点的名称?更新的JSFIDLE-
public class Test {
private static JSONObject processString(String data, int level) throws JSONException {
JSONObject json = new JSONObject();
int index = data.indexOf(',');
String name = data;
String remainder = "";
if (index < 0) {
index = name.indexOf('(');
if (index > 0) {
name = data.substring(0, index);
}
} else {
name = data.substring(0, index);
remainder = data.substring(name.length() + 1);
}
json.put("name", name);
JSONArray a = new JSONArray();
if (remainder.length() > 0) {
a.put(processString(remainder, level + 1));
json.put("T" + level, a);
} else {
json.put("leaf", a);
}
return json;
}
private static JSONArray processList(List<String> list, int level) throws JSONException {
JSONArray json = new JSONArray();
for (String data : list) {
json.put(processString(data, level));
}
return json;
}
private static JSONObject processMap(Map<String>, List<String>> map, int level) throws JSONException {
JSONObject json = new JSONObject();
for (String key : map.keySet()) {
json.put("name", key);
json.put("T" + level, processList(map.get(key), level + 1));
}
return json;
}
public static void main(String args[]) {
Map<String, List<String>> consilatedMapMap = new LinkedHashMap<String, List<String>>();
List<String> values = new LinkedList<String>();
values.add("Cone,KoolCool(21)");
values.add("Stick,KoolCool(25)");
consilatedMapMap.put("Ice creams", values);
try {
int level = 2;
JSONObject json = processMap(consilatedMapMap, level);
} catch(JSONException x) {
x.printStackTrace();
System.exit(-1);
}
}