String转Map 转Map

请问怎么把键值对的String转换成为map【java吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:618,120贴子:
请问怎么把键值对的String转换成为map收藏
{&error_code&:&5101001&,&error_ext_info&:&&,&error_info&:&消息内容为空&,&error_no&:&5101001&}比如这样的转换成map
2017java学习来上市公司博为峰学java,入学即签就业协议,不就业不收费,查看java课程!java好学吗?java课程来博为峰学,java工程师就业年薪十几万!
json转map,用一个包,包名我忘了,你只需要掉调用接口,这是最简单的方法,或者自己写一个,百度有的
遍历jsonobject的键值,put进map
Json2.jar善良,就像是感冒一样,是一种传染病。~~~
或者 jsonobject 把
登录百度帐号推荐应用map 转 二维数组 - 梦想飞翔的猫 - ITeye技术网站
博客分类:
在一个国外的站点无意间发现的 觉得挺有用 留着了
Map&String, String& map = new HashMap&String, String&();
map.put("key1", "value1");
map.put("key2", "value2");
Object[][] twoDarray = new String[map.size()][2];
Object[] keys = map.keySet().toArray();
Object[] values = map.values().toArray();
for (int row = 0; row & twoDarray. row++) {
twoDarray[row][0] = keys[row];
twoDarray[row][1] = values[row];
for (int i = 0; i & twoDarray. i++) {
for (int j = 0; j & twoDarray[i]. j++) {
System.out.println(twoDarray[i][j]);
浏览: 105018 次
来自: 大连
实现一个智能提示功能需要ajax、数据库、jsp/php、算法 ...
tinguo002 写道配置相对地址的时候 老出错,在这里找到 ...
配置相对地址的时候 老出错,在这里找到答案了,只是为什么 ,这 ...
太感谢了~ &env-entry-value&.. ...
Java_大猫 写道gwgyk 写道不写velocity.pr ...> Java点滴:Map与String的变换公共方法
Java点滴:Map与String的变换公共方法
william0130 & &
发布时间: & &
浏览:147 & &
回复:0 & &
悬赏:0.0希赛币
Java点滴:Map与String的转换公共方法  一个简单的Map与String的转换方法,比较实用的一段java代码
  1)将Map转成形如username'chenziwen^password'1234的字符串
* 方法名称:transMapToString
* 传入参数:map
* 返回值:String 形如 username'chenziwen^password'1234
public static String transMapToString(Map map){
java.util.Map.E
StringBuffer sb = new StringBuffer();
for(Iterator iterator = map.entrySet().iterator(); iterator.hasNext();)
entry = (java.util.Map.Entry)iterator.next();
sb.append(entry.getKey().toString()).append( &'& ).append(null==entry.getValue() &&:
entry.getValue().toString()).append (iterator.hasNext()
&^& : &&);
return sb.toString();
  2)将形如形如username'chenziwen^password'1234的字符串转成Map
* 方法名称:transStringToMap
* 传入参数:mapString 形如 username'chenziwen^password'1234
* 返回值:Map
public static Map transStringToMap(String mapString){
Map map = new HashMap();
java.util.StringT
for(StringTokenizer entrys = new StringTokenizer(mapString, &^&);entrys.hasMoreTokens();
map.put(items.nextToken(), items.hasMoreTokens()
((Object) (items.nextToken())) : null))
items = new StringTokenizer(entrys.nextToken(), &'&);
本问题标题:
本问题地址:
温馨提示:本问题已经关闭,不能解答。
暂无合适的专家
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&map,xml互转 - 张守磊 - ITeye技术网站
博客分类:
1.这个转出来会有很多空格package cn.paypalm.platform.sdk.
import java.lang.reflect.F
import java.util.C
import java.util.M
import java.util.S
* The Class MapToXMLString.
* @author zhangsl
public class MapToXMLString {
* Converter Map&Object, Object& instance to xml string. Note: currently,
* we aren't consider more about some collection types, such as array,list,
* @param dataMap
the data map
* @return the string
public static String converterPayPalm(Map&Object, Object& dataMap)
synchronized (MapToXMLString.class)
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("&?xml version='1.0' encoding='UTF-8' ?&");
strBuilder.append("&paypalm&");
Set&Object& objSet = dataMap.keySet();
for (Object key : objSet)
if (key == null)
strBuilder.append("\n");
strBuilder.append("&").append(key.toString()).append("&\n");
Object value = dataMap.get(key);
strBuilder.append(coverter(value).trim());
strBuilder.append("&/").append(key.toString()).append("&\n");
strBuilder.append("&/paypalm&");
return strBuilder.toString();
* Converter Map&Object, Object& instance to xml string. Note: currently,
* we aren't consider more about some collection types, such as array,list,
* @param dataMap
the data map
* @return the string
public static String converter(Map&Object, Object& dataMap, String xmlRootStart, String xmlRootEnd)
synchronized (MapToXMLString.class)
StringBuilder strBuilder = new StringBuilder();
strBuilder.append(xmlRootStart);
Set&Object& objSet = dataMap.keySet();
for (Object key : objSet)
if (key == null)
strBuilder.append("\n");
strBuilder.append("&").append(key.toString()).append("&\n");
Object value = dataMap.get(key);
strBuilder.append(coverter(value));
strBuilder.append("&/").append(key.toString()).append("&\n");
strBuilder.append(xmlRootEnd);
return strBuilder.toString();
public static String coverter(Object[] objects) {
StringBuilder strBuilder = new StringBuilder();
for(Object obj:objects) {
strBuilder.append("&item className=").append(obj.getClass().getName()).append("&\n");
strBuilder.append(coverter(obj));
strBuilder.append("&/item&\n");
return strBuilder.toString();
public static String coverter(Collection&?& objects)
StringBuilder strBuilder = new StringBuilder();
for(Object obj:objects) {
strBuilder.append("&item className=").append(obj.getClass().getName()).append("&\n");
strBuilder.append(coverter(obj));
strBuilder.append("&/item&\n");
return strBuilder.toString();
* Coverter.
* @param object the object
* @return the string
public static String coverter(Object object)
if (object instanceof Object[])
return coverter((Object[]) object);
if (object instanceof Collection)
return coverter((Collection&?&) object);
StringBuilder strBuilder = new StringBuilder();
if (isObject(object))
Class&? extends Object& clz = object.getClass();
Field[] fields = clz.getDeclaredFields();
for (Field field : fields)
field.setAccessible(true);
if (field == null)
String fieldName = field.getName();
Object value =
value = field.get(object);
catch (IllegalArgumentException e)
catch (IllegalAccessException e)
strBuilder.append("&").append(fieldName)
.append(" className=\"").append(
value.getClass().getName()).append("\"&\n");
if (isObject(value))
strBuilder.append(coverter(value));
else if (value == null)
strBuilder.append("null\n");
strBuilder.append(value.toString() + "\n");
strBuilder.append("&/").append(fieldName).append("&\n");
else if (object == null)
strBuilder.append("null\n");
strBuilder.append(object.toString() + "\n");
return strBuilder.toString();
* Checks if is object.
* @param obj the obj
* @return true, if is object
private static boolean isObject(Object obj)
if (obj == null)
if (obj instanceof String)
if (obj instanceof Integer)
if (obj instanceof Double)
if (obj instanceof Float)
if (obj instanceof Byte)
if (obj instanceof Long)
if (obj instanceof Character)
if (obj instanceof Short)
if (obj instanceof Boolean)
}
2.此用法出来的很正规,因为我不用xml中写type类型,所以自已改了原代码Map2XML xmlSerializer = new Map2XML();
String reqData = xmlSerializer.write(JSONObject.fromObject(map));
/*jadclipse*/// Decompiled by Jad v1.5.8f. Copyright 2001 Pavel Kouznetsov.
package net.sf.json.
import java.io.*;
import java.util.*;
import net.sf.json.*;
import net.sf.json.util.JSONU
import nu.xom.*;
import mons.lang.ArrayU
import mons.lang.StringU
import mons.logging.L
import mons.logging.LogF
public class Map2XML
private class XomSerializer extends Serializer
protected void write(Text text)
throws IOException
String value = text.getValue();
if(value.startsWith("&![CDATA[") && value.endsWith("]]&"))
value = value.substring(9);
value = value.substring(0, value.length() - 3);
writeRaw("&![CDATA[");
writeRaw(value);
writeRaw("]]&");
super.write(text);
protected void writeEmptyElementTag(Element element)
throws IOException
if((element instanceof CustomElement) && isNamespaceLenient())
writeTagBeginning((CustomElement)element);
writeRaw("/&");
super.writeEmptyElementTag(element);
protected void writeEndTag(Element element)
throws IOException
if((element instanceof CustomElement) && isNamespaceLenient())
writeRaw("&/");
writeRaw(((CustomElement)element).getQName());
writeRaw("&");
super.writeEndTag(element);
protected void writeNamespaceDeclaration(String prefix, String uri)
throws IOException
if(!StringUtils.isBlank(uri))
super.writeNamespaceDeclaration(prefix, uri);
protected void writeStartTag(Element element)
throws IOException
if((element instanceof CustomElement) && isNamespaceLenient())
writeTagBeginning((CustomElement)element);
writeRaw("&");
super.writeStartTag(element);
private void writeTagBeginning(CustomElement element)
throws IOException
writeRaw("&");
writeRaw(element.getQName());
writeAttributes(element);
writeNamespaceDeclarations(element);
public XomSerializer(OutputStream out)
super(out);
public XomSerializer(OutputStream out, String encoding)
throws UnsupportedEncodingException
super(out, encoding);
private static class CustomElement extends Element
private static String getName(String name)
int colon = name.indexOf(':');
if(colon != -1)
return name.substring(colon + 1);
private static String getPrefix(String name)
int colon = name.indexOf(':');
if(colon != -1)
return name.substring(0, colon);
return "";
public final String getQName()
if(prefix.length() == 0)
return getLocalName();
return prefix + ":" + getLocalName();
public CustomElement(String name)
super(getName(name));
prefix = getPrefix(name);
public Map2XML()
namespacesPerElement = new TreeMap();
rootNamespace = new TreeMap();
setObjectName("o");
setArrayName("a");
setElementName("e");
setTypeHintsEnabled(true);
setTypeHintsCompatibility(true);
setNamespaceLenient(false);
setSkipNamespaces(false);
setRemoveNamespacePrefixFromElements(false);
setTrimSpaces(false);
setExpandableProperties(EMPTY_ARRAY);
setSkipNamespaces(false);
public void addNamespace(String prefix, String uri)
addNamespace(prefix, uri, null);
public void addNamespace(String prefix, String uri, String elementName)
if(StringUtils.isBlank(uri))
if(prefix == null)
prefix = "";
if(StringUtils.isBlank(elementName))
rootNamespace.put(prefix.trim(), uri.trim());
Map nameSpaces = (Map)namespacesPerElement.get(elementName);
if(nameSpaces == null)
nameSpaces = new TreeMap();
namespacesPerElement.put(elementName, nameSpaces);
nameSpaces.put(prefix, uri);
public void clearNamespaces()
rootNamespace.clear();
namespacesPerElement.clear();
public void clearNamespaces(String elementName)
if(StringUtils.isBlank(elementName))
rootNamespace.clear();
namespacesPerElement.remove(elementName);
public String getArrayName()
return arrayN
public String getElementName()
return elementN
public String[] getExpandableProperties()
return expandableP
public String getObjectName()
return objectN
public String getRootName()
return rootN
public boolean isForceTopLevelObject()
return forceTopLevelO
public boolean isNamespaceLenient()
return namespaceL
public boolean isRemoveNamespacePrefixFromElements()
return removeNamespacePrefixFromE
public boolean isSkipNamespaces()
return skipN
public boolean isSkipWhitespace()
return skipW
public boolean isTrimSpaces()
return trimS
public boolean isTypeHintsCompatibility()
return typeHintsC
public boolean isTypeHintsEnabled()
return typeHintsE
public JSON read(String xml) throws ValidityException, ParsingException, IOException
JSON json =
Document doc = (new Builder()).build(new StringReader(xml));
root = doc.getRootElement();
if(isNullObject(root))
return JSONNull.getInstance();
String defaultType = getType(root, "string");
if(isArray(root, true))
json = processArrayElement(root, defaultType);
if(forceTopLevelObject)
String key = removeNamespacePrefix(root.getQualifiedName());
json = (new JSONObject()).element(key, json);
json = processObjectElement(root, defaultType);
if(forceTopLevelObject)
String key = removeNamespacePrefix(root.getQualifiedName());
json = (new JSONObject()).element(key, json);
catch(JSONException jsone)
catch(Exception e)
throw new JSONException(e);
public JSON readFromFile(File file) throws ValidityException, ParsingException, IOException
if(file == null)
throw new JSONException("File is null");
if(!file.canRead())
throw new JSONException("Can't read input file");
if(file.isDirectory())
throw new JSONException("File is a directory");
return readFromStream(new FileInputStream(file));
public JSON readFromFile(String path) throws ValidityException, ParsingException, IOException
return readFromStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(path));
public JSON readFromStream(InputStream stream) throws ValidityException, ParsingException, IOException
xml = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
for(String line = (line = in.readLine()) !=)
xml.append(line);
return read(xml.toString());
public void removeNamespace(String prefix)
removeNamespace(prefix, null);
public void removeNamespace(String prefix, String elementName)
if(prefix == null)
prefix = "";
if(StringUtils.isBlank(elementName))
rootNamespace.remove(prefix.trim());
Map nameSpaces = (Map)namespacesPerElement.get(elementName);
nameSpaces.remove(prefix);
public void setArrayName(String arrayName)
this.arrayName = StringUtils.isBlank(arrayName) ? "a" : arrayN
public void setElementName(String elementName)
this.elementName = StringUtils.isBlank(elementName) ? "e" : elementN
public void setExpandableProperties(String expandableProperties[])
this.expandableProperties = expandableProperties != null ? expandableProperties : EMPTY_ARRAY;
public void setForceTopLevelObject(boolean forceTopLevelObject)
this.forceTopLevelObject = forceTopLevelO
public void setNamespace(String prefix, String uri)
setNamespace(prefix, uri, null);
public void setNamespace(String prefix, String uri, String elementName)
if(StringUtils.isBlank(uri))
if(prefix == null)
prefix = "";
if(StringUtils.isBlank(elementName))
rootNamespace.clear();
rootNamespace.put(prefix.trim(), uri.trim());
Map nameSpaces = (Map)namespacesPerElement.get(elementName);
if(nameSpaces == null)
nameSpaces = new TreeMap();
namespacesPerElement.put(elementName, nameSpaces);
nameSpaces.clear();
nameSpaces.put(prefix, uri);
public void setNamespaceLenient(boolean namespaceLenient)
this.namespaceLenient = namespaceL
public void setObjectName(String objectName)
this.objectName = StringUtils.isBlank(objectName) ? "o" : objectN
public void setRemoveNamespacePrefixFromElements(boolean removeNamespacePrefixFromElements)
this.removeNamespacePrefixFromElements = removeNamespacePrefixFromE
public void setRootName(String rootName)
this.rootName = StringUtils.isBlank(rootName) ? null : rootN
public void setSkipNamespaces(boolean skipNamespaces)
this.skipNamespaces = skipN
public void setSkipWhitespace(boolean skipWhitespace)
this.skipWhitespace = skipW
public void setTrimSpaces(boolean trimSpaces)
this.trimSpaces = trimS
public void setTypeHintsCompatibility(boolean typeHintsCompatibility)
this.typeHintsCompatibility = typeHintsC
public void setTypeHintsEnabled(boolean typeHintsEnabled)
this.typeHintsEnabled = typeHintsE
public String write(JSON json)
return write(json, null);
public String write(JSON json, String encoding)
if(JSONNull.getInstance().equals(json))
Element root =
root = newElement(getRootName() != null ? getRootName() : getObjectName());
root.addAttribute(new Attribute(addJsonPrefix("null"), "true"));
Document doc = new Document(root);
return writeDocument(doc, encoding);
if(json instanceof JSONArray)
JSONArray jsonArray = (JSONArray)
root = processJSONArray(jsonArray, newElement(getRootName() != null ? getRootName() : getArrayName()), expandableProperties);
doc = new Document(root);
return writeDocument(doc, encoding);
JSONObject jsonObject = (JSONObject)
if(jsonObject.isNullObject())
root = newElement(getObjectName());
root.addAttribute(new Attribute(addJsonPrefix("null"), "true"));
root = processJSONObject(jsonObject, newElement(getRootName() != null ? getRootName() : getObjectName()), expandableProperties, true);
doc = new Document(root);
return writeDocument(doc, encoding);
private String addJsonPrefix(String str)
if(!isTypeHintsCompatibility())
return "json_" +
private void addNameSpaceToElement(Element element)
String elementName =
if(element instanceof CustomElement)
elementName = ((CustomElement)element).getQName();
elementName = element.getQualifiedName();
Map nameSpaces = (Map)namespacesPerElement.get(elementName);
if(nameSpaces != null && !nameSpaces.isEmpty())
setNamespaceLenient(true);
for(Iterator entries = nameSpaces.entrySet().iterator(); entries.hasNext();)
java.util.Map.Entry entry = (java.util.Map.Entry)entries.next();
String prefix = (String)entry.getKey();
String uri = (String)entry.getValue();
if(StringUtils.isBlank(prefix))
element.setNamespaceURI(uri);
element.addNamespaceDeclaration(prefix, uri);
private boolean checkChildElements(Element element, boolean isTopLevel)
int childCount = element.getChildCount();
Elements elements = element.getChildElements();
int elementCount = elements.size();
if(childCount == 1 && (element.getChild(0) instanceof Text))
return isTopL
if(childCount == elementCount)
if(elementCount == 0)
if(elementCount == 1)
return skipWhitespace || (element.getChild(0) instanceof Text);
if(childCount & elementCount)
for(int i = 0; i & childC i++)
nu.xom.Node node = element.getChild(i);
if(!(node instanceof Text))
Text text = (Text)
if(StringUtils.isNotBlank(StringUtils.strip(text.getValue())) && !skipWhitespace)
String childName = elements.get(0).getQualifiedName();
for(int i = 1; i & elementC i++)
pareTo(elements.get(i).getQualifiedName()) != 0)
private String getClass(Element element)
Attribute attribute = element.getAttribute(addJsonPrefix("class"));
String clazz =
if(attribute != null)
String clazzText = attribute.getValue().trim();
if("object".compareToIgnoreCase(clazzText) == 0)
clazz = "object";
if("array".compareToIgnoreCase(clazzText) == 0)
clazz = "array";
private String getType(Element element)
return getType(element, null);
private String getType(Element element, String defaultType)
Attribute attribute = element.getAttribute(addJsonPrefix("type"));
String type =
if(attribute != null)
String typeText = attribute.getValue().trim();
if("boolean".compareToIgnoreCase(typeText) == 0)
type = "boolean";
if("number".compareToIgnoreCase(typeText) == 0)
type = "number";
if("integer".compareToIgnoreCase(typeText) == 0)
type = "integer";
if("float".compareToIgnoreCase(typeText) == 0)
type = "float";
if("object".compareToIgnoreCase(typeText) == 0)
type = "object";
if("array".compareToIgnoreCase(typeText) == 0)
type = "array";
if("string".compareToIgnoreCase(typeText) == 0)
type = "string";
if("function".compareToIgnoreCase(typeText) == 0)
type = "function";
if(defaultType != null)
("Using default type " + defaultType);
type = defaultT
private boolean hasNamespaces(Element element)
int namespaces = 0;
for(int i = 0; i & element.getNamespaceDeclarationCount(); i++)
String prefix = element.getNamespacePrefix(i);
String uri = element.getNamespaceURI(prefix);
if(!StringUtils.isBlank(uri))
namespaces++;
return namespaces & 0;
private boolean isArray(Element element, boolean isTopLevel)
boolean isArray =
String clazz = getClass(element);
if(clazz != null && clazz.equals("array"))
if(element.getAttributeCount() == 0)
isArray = checkChildElements(element, isTopLevel);
if(element.getAttributeCount() == 1 && (element.getAttribute(addJsonPrefix("class")) != null || element.getAttribute(addJsonPrefix("type")) != null))
isArray = checkChildElements(element, isTopLevel);
if(element.getAttributeCount() == 2 && element.getAttribute(addJsonPrefix("class")) != null && element.getAttribute(addJsonPrefix("type")) != null)
isArray = checkChildElements(element, isTopLevel);
if(isArray)
for(int j = 0; j & element.getNamespaceDeclarationCount(); j++)
String prefix = element.getNamespacePrefix(j);
String uri = element.getNamespaceURI(prefix);
if(!StringUtils.isBlank(uri))
return isA
private boolean isFunction(Element element)
int attrCount = element.getAttributeCount();
if(attrCount & 0)
Attribute typeAttr = element.getAttribute(addJsonPrefix("type"));
Attribute paramsAttr = element.getAttribute(addJsonPrefix("params"));
if(attrCount == 1 && paramsAttr != null)
if(attrCount == 2 && paramsAttr != null && typeAttr != null && (typeAttr.getValue().compareToIgnoreCase("string") == 0 || typeAttr.getValue().compareToIgnoreCase("function") == 0))
private boolean isNullObject(Element element)
if(element.getChildCount() == 0)
if(element.getAttributeCount() == 0)
if(element.getAttribute(addJsonPrefix("null")) != null)
if(element.getAttributeCount() == 1 && (element.getAttribute(addJsonPrefix("class")) != null || element.getAttribute(addJsonPrefix("type")) != null))
if(element.getAttributeCount() == 2 && element.getAttribute(addJsonPrefix("class")) != null && element.getAttribute(addJsonPrefix("type")) != null)
return skipWhitespace && element.getChildCount() == 1 && (element.getChild(0) instanceof Text);
private boolean isObject(Element element, boolean isTopLevel)
boolean isObject =
if(!isArray(element, isTopLevel) && !isFunction(element))
if(hasNamespaces(element))
int attributeCount = element.getAttributeCount();
if(attributeCount & 0)
int attrs = element.getAttribute(addJsonPrefix("null")) != null ? 1 : 0;
attrs += element.getAttribute(addJsonPrefix("class")) != null ? 1 : 0;
attrs += element.getAttribute(addJsonPrefix("type")) != null ? 1 : 0;
switch(attributeCount)
case 1: // '\001'
if(attrs == 0)
case 2: // '\002'
if(attrs & 2)
case 3: // '\003'
if(attrs & 3)
int childCount = element.getChildCount();
if(childCount == 1 && (element.getChild(0) instanceof Text))
return isTopL
isObject =
return isO
private Element newElement(String name)
if(name.indexOf(':') != -1)
namespaceLenient =
return ((Element) (namespaceLenient ? new CustomElement(name) : new Element(name)));
private JSON processArrayElement(Element element, String defaultType)
JSONArray jsonArray = new JSONArray();
int childCount = element.getChildCount();
for(int i = 0; i & childC i++)
nu.xom.Node child = element.getChild(i);
if(child instanceof Text)
Text text = (Text)
if(StringUtils.isNotBlank(StringUtils.strip(text.getValue())))
jsonArray.element(text.getValue());
if(child instanceof Element)
setValue(jsonArray, (Element)child, defaultType);
return jsonA
private Object processElement(Element element, String type)
if(isNullObject(element))
return JSONNull.getInstance();
if(isArray(element, false))
return processArrayElement(element, type);
if(isObject(element, false))
return processObjectElement(element, type);
return trimSpaceFromValue(element.getValue());
private Element processJSONArray(JSONArray array, Element root, String expandableProperties[])
int l = array.size();
for(int i = 0; i & i++)
Object value = array.get(i);
Element element = processJSONValue(value, root, null, expandableProperties);
root.appendChild(element);
private Element processJSONObject(JSONObject jsonObject, Element root, String expandableProperties[], boolean isRoot)
if(jsonObject.isNullObject())
root.addAttribute(new Attribute(addJsonPrefix("null"), "true"));
if(jsonObject.isEmpty())
if(isRoot && !rootNamespace.isEmpty())
setNamespaceLenient(true);
for(Iterator entries = rootNamespace.entrySet().iterator(); entries.hasNext();)
java.util.Map.Entry entry = (java.util.Map.Entry)entries.next();
String prefix = (String)entry.getKey();
String uri = (String)entry.getValue();
if(StringUtils.isBlank(prefix))
root.setNamespaceURI(uri);
root.addNamespaceDeclaration(prefix, uri);
addNameSpaceToElement(root);
Object names[] = jsonObject.names().toArray();
Arrays.sort(names);
Element element =
for(int i = 0; i & names. i++)
String name = (String)names[i];
Object value = jsonObject.get(name);
if(name.startsWith("@xmlns"))
setNamespaceLenient(true);
int colon = name.indexOf(':');
if(colon == -1)
if(StringUtils.isBlank(root.getNamespaceURI()))
root.setNamespaceURI(String.valueOf(value));
String prefix = name.substring(colon + 1);
if(StringUtils.isBlank(root.getNamespaceURI(prefix)))
root.addNamespaceDeclaration(prefix, String.valueOf(value));
if(name.startsWith("@"))
root.addAttribute(new Attribute(name.substring(1), String.valueOf(value)));
if(name.equals("#text"))
if(value instanceof JSONArray)
root.appendChild(((JSONArray)value).join("", true));
root.appendChild(String.valueOf(value));
if((value instanceof JSONArray) && (((JSONArray)value).isExpandElements() || ArrayUtils.contains(expandableProperties, name)))
JSONArray array = (JSONArray)
int l = array.size();
int j = 0;
if(j &= l)
continue label0;
Object item = array.get(j);
element = newElement(name);
if(item instanceof JSONObject)
element = processJSONValue((JSONObject)item, root, element, expandableProperties);
if(item instanceof JSONArray)
element = processJSONValue((JSONArray)item, root, element, expandableProperties);
element = processJSONValue(item, root, element, expandableProperties);
addNameSpaceToElement(element);
root.appendChild(element);
} while(true);
element = newElement(name);
element = processJSONValue(value, root, element, expandableProperties);
addNameSpaceToElement(element);
root.appendChild(element);
private Element processJSONValue(Object value, Element root, Element target, String expandableProperties[])
if(target == null)
target = newElement(getElementName());
if(JSONUtils.isBoolean(value))
if(isTypeHintsEnabled())
target.addAttribute(new Attribute(addJsonPrefix("type"), "boolean"));
target.appendChild(value.toString());
if(JSONUtils.isNumber(value))
// if(isTypeHintsEnabled())
target.addAttribute(new Attribute(addJsonPrefix("type"), "number"));
target.appendChild(value.toString());
if(JSONUtils.isFunction(value))
if(value instanceof String)
value = JSONFunction.parse((String)value);
JSONFunction func = (JSONFunction)
if(isTypeHintsEnabled())
target.addAttribute(new Attribute(addJsonPrefix("type"), "function"));
String params = ArrayUtils.toString(func.getParams());
params = params.substring(1);
params = params.substring(0, params.length() - 1);
target.addAttribute(new Attribute(addJsonPrefix("params"), params));
target.appendChild(new Text("&![CDATA[" + func.getText() + "]]&"));
if(JSONUtils.isString(value))
if(isTypeHintsEnabled())
target.addAttribute(new Attribute(addJsonPrefix("type"), "string"));
target.appendChild(value.toString());
if(value instanceof JSONArray)
if(isTypeHintsEnabled())
target.addAttribute(new Attribute(addJsonPrefix("class"), "array"));
target = processJSONArray((JSONArray)value, target, expandableProperties);
if(value instanceof JSONObject)
if(isTypeHintsEnabled())
target.addAttribute(new Attribute(addJsonPrefix("class"), "object"));
target = processJSONObject((JSONObject)value, target, expandableProperties, false);
if(JSONUtils.isNull(value))
if(isTypeHintsEnabled())
target.addAttribute(new Attribute(addJsonPrefix("class"), "object"));
target.addAttribute(new Attribute(addJsonPrefix("null"), "true"));
private JSON processObjectElement(Element element, String defaultType)
if(isNullObject(element))
return JSONNull.getInstance();
JSONObject jsonObject = new JSONObject();
if(!skipNamespaces)
for(int j = 0; j & element.getNamespaceDeclarationCount(); j++)
String prefix = element.getNamespacePrefix(j);
String uri = element.getNamespaceURI(prefix);
if(StringUtils.isBlank(uri))
if(!StringUtils.isBlank(prefix))
prefix = ":" +
setOrAccumulate(jsonObject, "@xmlns" + prefix, trimSpaceFromValue(uri));
int attrCount = element.getAttributeCount();
for(int i = 0; i & attrC i++)
Attribute attr = element.getAttribute(i);
String attrname = attr.getQualifiedName();
if(!isTypeHintsEnabled() || addJsonPrefix("class").compareToIgnoreCase(attrname) != 0 && addJsonPrefix("type").compareToIgnoreCase(attrname) != 0)
String attrvalue = attr.getValue();
setOrAccumulate(jsonObject, "@" + removeNamespacePrefix(attrname), trimSpaceFromValue(attrvalue));
int childCount = element.getChildCount();
for(int i = 0; i & childC i++)
nu.xom.Node child = element.getChild(i);
if(child instanceof Text)
Text text = (Text)
if(StringUtils.isNotBlank(StringUtils.strip(text.getValue())))
setOrAccumulate(jsonObject, "#text", trimSpaceFromValue(text.getValue()));
if(child instanceof Element)
setValue(jsonObject, (Element)child, defaultType);
return jsonO
private String removeNamespacePrefix(String name)
if(isRemoveNamespacePrefixFromElements())
int colon = name.indexOf(':');
return colon == -1 ? name : name.substring(colon + 1);
private void setOrAccumulate(JSONObject jsonObject, String key, Object value)
if(jsonObject.has(key))
jsonObject.accumulate(key, value);
Object val = jsonObject.get(key);
if(val instanceof JSONArray)
((JSONArray)val).setExpandElements(true);
jsonObject.element(key, value);
private void setValue(JSONArray jsonArray, Element element, String defaultType)
String clazz = getClass(element);
String type = getType(element);
type = type != null ? type : defaultT
if(hasNamespaces(element) && !skipNamespaces)
jsonArray.element(simplifyValue(null, processElement(element, type)));
if(element.getAttributeCount() & 0)
if(isFunction(element))
Attribute paramsAttribute = element.getAttribute(addJsonPrefix("params"));
String params[] =
String text = element.getValue();
params = StringUtils.split(paramsAttribute.getValue(), ",");
jsonArray.element(new JSONFunction(params, text));
jsonArray.element(simplifyValue(null, processElement(element, type)));
boolean classProcessed =
if(clazz != null)
pareToIgnoreCase("array") == 0)
jsonArray.element(processArrayElement(element, type));
classProcessed =
pareToIgnoreCase("object") == 0)
jsonArray.element(simplifyValue(null, processObjectElement(element, type)));
classProcessed =
if(!classProcessed)
pareToIgnoreCase("boolean") == 0)
jsonArray.element(Boolean.valueOf(element.getValue()));
pareToIgnoreCase("number") == 0)
jsonArray.element(Integer.valueOf(element.getValue()));
catch(NumberFormatException e)
jsonArray.element(Double.valueOf(element.getValue()));
pareToIgnoreCase("integer") == 0)
jsonArray.element(Integer.valueOf(element.getValue()));
pareToIgnoreCase("float") == 0)
jsonArray.element(Double.valueOf(element.getValue()));
pareToIgnoreCase("function") == 0)
String params[] =
String text = element.getValue();
Attribute paramsAttribute = element.getAttribute(addJsonPrefix("params"));
if(paramsAttribute != null)
params = StringUtils.split(paramsAttribute.getValue(), ",");
jsonArray.element(new JSONFunction(params, text));
pareToIgnoreCase("string") == 0)
Attribute paramsAttribute = element.getAttribute(addJsonPrefix("params"));
if(paramsAttribute != null)
String params[] =
String text = element.getValue();
params = StringUtils.split(paramsAttribute.getValue(), ",");
jsonArray.element(new JSONFunction(params, text));
if(isArray(element, false))
jsonArray.element(processArrayElement(element, defaultType));
if(isObject(element, false))
jsonArray.element(simplifyValue(null, processObjectElement(element, defaultType)));
jsonArray.element(trimSpaceFromValue(element.getValue()));
private void setValue(JSONObject jsonObject, Element element, String defaultType)
String clazz = getClass(element);
String type = getType(element);
type = type != null ? type : defaultT
String key = removeNamespacePrefix(element.getQualifiedName());
if(hasNamespaces(element) && !skipNamespaces)
setOrAccumulate(jsonObject, key, simplifyValue(jsonObject, processElement(element, type)));
if(element.getAttributeCount() & 0 && isFunction(element))
Attribute paramsAttribute = element.getAttribute(addJsonPrefix("params"));
String text = element.getValue();
String params[] = StringUtils.split(paramsAttribute.getValue(), ",");
setOrAccumulate(jsonObject, key, new JSONFunction(params, text));
boolean classProcessed =
if(clazz != null)
pareToIgnoreCase("array") == 0)
setOrAccumulate(jsonObject, key, processArrayElement(element, type));
classProcessed =
pareToIgnoreCase("object") == 0)
setOrAccumulate(jsonObject, key, simplifyValue(jsonObject, processObjectElement(element, type)));
classProcessed =
if(!classProcessed)
pareToIgnoreCase("boolean") == 0)
setOrAccumulate(jsonObject, key, Boolean.valueOf(element.getValue()));
pareToIgnoreCase("number") == 0)
setOrAccumulate(jsonObject, key, Integer.valueOf(element.getValue()));
catch(NumberFormatException e)
setOrAccumulate(jsonObject, key, Double.valueOf(element.getValue()));
pareToIgnoreCase("integer") == 0)
setOrAccumulate(jsonObject, key, Integer.valueOf(element.getValue()));
pareToIgnoreCase("float") == 0)
setOrAccumulate(jsonObject, key, Double.valueOf(element.getValue()));
pareToIgnoreCase("function") == 0)
String params[] =
String text = element.getValue();
Attribute paramsAttribute = element.getAttribute(addJsonPrefix("params"));
if(paramsAttribute != null)
params = StringUtils.split(paramsAttribute.getValue(), ",");
setOrAccumulate(jsonObject, key, new JSONFunction(params, text));
pareToIgnoreCase("string") == 0)
Attribute paramsAttribute = element.getAttribute(addJsonPrefix("params"));
if(paramsAttribute != null)
String params[] =
String text = element.getValue();
params = StringUtils.split(paramsAttribute.getValue(), ",");
setOrAccumulate(jsonObject, key, new JSONFunction(params, text));
if(isArray(element, false))
setOrAccumulate(jsonObject, key, processArrayElement(element, defaultType));
if(isObject(element, false))
setOrAccumulate(jsonObject, key, simplifyValue(jsonObject, processObjectElement(element, defaultType)));
setOrAccumulate(jsonObject, key, trimSpaceFromValue(element.getValue()));
private Object simplifyValue(JSONObject parent, Object json)
if(json instanceof JSONObject)
JSONObject object = (JSONObject)
if(parent != null)
Iterator entries = parent.entrySet().iterator();
if(!entries.hasNext())
java.util.Map.Entry entry = (java.util.Map.Entry)entries.next();
String key = (String)entry.getKey();
Object value = entry.getValue();
if(key.startsWith("@xmlns") && value.equals(object.opt(key)))
object.remove(key);
} while(true);
if(object.size() == 1 && object.has("#text"))
return object.get("#text");
private String trimSpaceFromValue(String value)
if(isTrimSpaces())
return value.trim();
private String writeDocument(Document doc, String encoding)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XomSerializer serializer = encoding != null ? new XomSerializer(baos, encoding) : new XomSerializer(baos);
serializer.write(doc);
encoding = serializer.getEncoding();
catch(IOException ioe)
throw new JSONException(ioe);
String str =
str = baos.toString(encoding);
catch(UnsupportedEncodingException uee)
throw new JSONException(uee);
static Class _mthclass$(String x0) throws ClassNotFoundException
return Class.forName(x0);
private static final String EMPTY_ARRAY[] = new String[0];
private static final String JSON_PREFIX = "json_";
private static final L
private String arrayN
private String elementN
private String expandableProperties[];
private boolean forceTopLevelO
private boolean namespaceL
private Map namespacesPerE
private String objectN
private boolean removeNamespacePrefixFromE
private String rootN
private Map rootN
private boolean skipN
private boolean skipW
private boolean trimS
private boolean typeHintsC
private boolean typeHintsE
log = LogFactory.getLog(net.sf.json.xml.Map2XML.class);
DECOMPILATION REPORT
Decompiled from: D:\maven3.0.4\net\sf\json-lib\json-lib\2.4\json-lib-2.4-jdk15.jar
Total time: 472 ms
Jad reported messages/errors:
Couldn't resolve all exception handlers in method read
Couldn't fully decompile method readFromFile
Couldn't resolve all exception handlers in method readFromFile
Couldn't fully decompile method readFromStream
Couldn't resolve all exception handlers in method readFromStream
Couldn't fully decompile method _mthclass$
Couldn't resolve all exception handlers in method _mthclass$
Exit status: 0
Caught exceptions:
浏览: 362261 次
来自: 天津
我当然不能全写上面了,这只是其中一部分https连接。
大哥,这是双向认证吗?
例如以下代码能遍历字符串&Tom:Mi ...
2*3*5=30,是30个以上的请求才拒绝呀。
2台跟1台一样的效果。

我要回帖

更多关于 Map转list 的文章

 

随机推荐