org.json.XML.toJSONObject()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(377)

本文整理了Java中org.json.XML.toJSONObject()方法的一些代码示例,展示了XML.toJSONObject()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XML.toJSONObject()方法的具体详情如下:
包路径:org.json.XML
类名称:XML
方法名:toJSONObject

XML.toJSONObject介绍

[英]Convert a well-formed (but not necessarily valid) XML into a JSONObject. Some information may be lost in this transformation because JSON is a data format and XML is a document format. XML uses elements, attributes, and content text, while JSON uses unordered collections of name/value pairs and arrays of values. JSON does not does not like to distinguish between elements and attributes. Sequences of similar elements are represented as JSONArrays. Content text may be placed in a "content" member. Comments, prologs, DTDs, and <[ [ ]]> are ignored.
[中]将格式良好(但不一定有效)的XML转换为JSONObject。这种转换可能会丢失一些信息,因为JSON是一种数据格式,而XML是一种文档格式。XML使用元素、属性和内容文本,而JSON使用无序的名称/值对集合和值数组。JSON不喜欢区分元素和属性。类似元素的序列表示为JSONArray。内容文本可以放在“内容”成员中。注释、序言、DTD和<[ [ ]]>将被忽略。

代码示例

代码示例来源:origin: loklak/loklak_server

/**
 * Convert a well-formed (but not necessarily valid) XML string into a
 * JSONObject. Some information may be lost in this transformation because
 * JSON is a data format and XML is a document format. XML uses elements,
 * attributes, and content text, while JSON uses unordered collections of
 * name/value pairs and arrays of values. JSON does not does not like to
 * distinguish between elements and attributes. Sequences of similar
 * elements are represented as JSONArrays. Content text may be placed in a
 * "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code>
 * are ignored.
 * 
 * @param string
 *            The source string.
 * @return A JSONObject containing the structured data from the XML string.
 * @throws JSONException Thrown if there is an errors while parsing the string
 */
public static JSONObject toJSONObject(String string) throws JSONException {
  return toJSONObject(string, false);
}

代码示例来源:origin: loklak/loklak_server

transformer.transform(domSource, result);
JSONObject xmlresult = new JSONObject(true);
xmlresult = XML.toJSONObject(writer.toString());
JSONObject items = xmlresult.getJSONObject(operation).getJSONObject("Items");
if (items.getJSONObject("Request").has("Errors")) {

代码示例来源:origin: loklak/loklak_server

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  Query post = RemoteAccess.evaluate(request);
  // manage DoS
  if (post.isDoS_blackout()) {response.sendError(503, "your request frequency is too high"); return;}
  // evaluate get parameters
  String data = post.get("data", "");
  try {
    String jsonData = XML.toJSONObject(data).toString();
    JSONObject json = new JSONObject(jsonData);
    PrintWriter sos = response.getWriter();
    sos.print(json.toString(2));
    sos.println();
  }
  catch (IOException e) {
    DAO.severe(e);
    JSONObject json = new JSONObject(true);
    json.put("error", "Malformed XML. Please check XML Again");
    json.put("type", "Error");
    PrintWriter sos = response.getWriter();
    sos.print(json.toString(2));
    sos.println();
  }
  post.finalize();
}

代码示例来源:origin: loklak/loklak_server

JSONObject json = XML.toJSONObject(content);
PrintWriter sos = response.getWriter();
JSONObject resourceObject = json.getJSONObject("resources");

代码示例来源:origin: b3log/latke

/**
 * Convert a well-formed (but not necessarily valid) XML string into a
 * JSONObject. Some information may be lost in this transformation because
 * JSON is a data format and XML is a document format. XML uses elements,
 * attributes, and content text, while JSON uses unordered collections of
 * name/value pairs and arrays of values. JSON does not does not like to
 * distinguish between elements and attributes. Sequences of similar
 * elements are represented as JSONArrays. Content text may be placed in a
 * "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code>
 * are ignored.
 * 
 * @param string
 *            The source string.
 * @return A JSONObject containing the structured data from the XML string.
 * @throws JSONException Thrown if there is an errors while parsing the string
 */
public static JSONObject toJSONObject(String string) throws JSONException {
  return toJSONObject(string, false);
}

代码示例来源:origin: b3log/latke

/**
 * Convert a well-formed (but not necessarily valid) XML into a
 * JSONObject. Some information may be lost in this transformation because
 * JSON is a data format and XML is a document format. XML uses elements,
 * attributes, and content text, while JSON uses unordered collections of
 * name/value pairs and arrays of values. JSON does not does not like to
 * distinguish between elements and attributes. Sequences of similar
 * elements are represented as JSONArrays. Content text may be placed in a
 * "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code>
 * are ignored.
 *
 * @param reader The XML source reader.
 * @return A JSONObject containing the structured data from the XML string.
 * @throws JSONException Thrown if there is an errors while parsing the string
 */
public static JSONObject toJSONObject(Reader reader) throws JSONException {
  return toJSONObject(reader, false);
}

代码示例来源:origin: b3log/latke

/**
 * Convert a well-formed (but not necessarily valid) XML string into a
 * JSONObject. Some information may be lost in this transformation because
 * JSON is a data format and XML is a document format. XML uses elements,
 * attributes, and content text, while JSON uses unordered collections of
 * name/value pairs and arrays of values. JSON does not does not like to
 * distinguish between elements and attributes. Sequences of similar
 * elements are represented as JSONArrays. Content text may be placed in a
 * "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code>
 * are ignored.
 * 
 * All values are converted as strings, for 1, 01, 29.0 will not be coerced to
 * numbers but will instead be the exact value as seen in the XML document.
 * 
 * @param string
 *            The source string.
 * @param keepStrings If true, then values will not be coerced into boolean
 *  or numeric values and will instead be left as strings
 * @return A JSONObject containing the structured data from the XML string.
 * @throws JSONException Thrown if there is an errors while parsing the string
 */
public static JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
  return toJSONObject(new StringReader(string), keepStrings);
}

代码示例来源:origin: tranxactive/J2PAY

/**
 * This method converts xml into JSONObject.
 *
 * @param xml the xml string than will be parsed.
 * @return the JSONObject json representation of xml
 */
public static JSONObject toJson(String xml) {
  return XML.toJSONObject(xml);
}

代码示例来源:origin: amzn/amazon-pay-sdk-java

/**
 * Converts the raw XML response to JSON format
 *
 * @throws JSONException
 *          If the XML response cannot be converted into JSON
 * @return
 *          JSON representation of response returned by Amazon Service API
 */
public String toJSON() throws JSONException {
  JSONObject jsonObj = XML.toJSONObject(this.responseXml);
  return jsonObj.toString();
}

代码示例来源:origin: usc-isi-i2/Web-Karma

public static String getJsonFromXml(String xmlStr, boolean prettyOutput) {
    org.json.JSONObject xmlJSONObj = XML.toJSONObject(xmlStr);
    String jsonStr = "";
    if(prettyOutput)
      jsonStr = xmlJSONObj.toString(4);
    else
      jsonStr = xmlJSONObj.toString();
    
    
    return jsonStr;
  }
}

代码示例来源:origin: com.jdcloud.sdk/core

public static String xmlToJson(InputStream is) throws JAXBException, IOException {
  String xml = IOUtils.toString(is, Charset.forName("UTF-8"));
  JSONObject xmlJSONObj = XML.toJSONObject(xml);
  return xmlJSONObj.toString();
}

代码示例来源:origin: tmobile/pacbot

/**
 * @param response
 * @return
 */
private boolean sniffPublicAccess(String response) {
  JSONObject jsonObject = XML.toJSONObject(response);
  return "NoSuchKey".equals(jsonObject.getJSONObject("Error").get("Code"));
}

代码示例来源:origin: usc-isi-i2/Web-Karma

public static String convertXML2JSON(String xmlContent) {
    try {
//            XMLSerializer xmlSerializer = new XMLSerializer();
//            JSON json = xmlSerializer.read( xmlContent );
//            return json.toString();
      JSONObject jsonObj =  XML.toJSONObject(xmlContent);
      return jsonObj.toString();
    } catch (Exception e) {
      System.out.println(e.getMessage());
      return null;
    }
    
  }

代码示例来源:origin: yaoguangluo/DETA_BackEnd

public Map<String, Object> XmlToMap(String response){
  JSONObject responseJson = XML.toJSONObject(response);
  Gson gson = new Gson();
  Type type = new TypeToken<Map<String, Object>>(){}.getType();
  Map<String, Object> responseMap =gson.fromJson(responseJson.toString(), type);
  return responseMap;
}

代码示例来源:origin: yaoguangluo/DETA_CACHE

public Map<String, Object> XmlToMap(String response){
  JSONObject responseJson = XML.toJSONObject(response);
  Gson gson = new Gson();
  Type type = new TypeToken<Map<String, Object>>(){}.getType();
  Map<String, Object> responseMap =gson.fromJson(responseJson.toString(), type);
  return responseMap;
}

代码示例来源:origin: org.streampipes/streampipes-connect

@Override
public void parse(InputStream data, EmitBinaryEvent emitBinaryEvent) {
  try {
    String dataString = CharStreams.toString(new InputStreamReader(data, Charsets.UTF_8));
    JSONObject xmlJSONObj = XML.toJSONObject(dataString);
    searchAndEmitEvents(xmlJSONObj.toMap(), tag, emitBinaryEvent);
  } catch (JSONException e) {
    logger.error(e.toString());
  } catch (IOException e) {
    logger.error(e.toString());
  }
}

代码示例来源:origin: streampipes/streampipes-ce

@Override
public void parse(InputStream data, EmitBinaryEvent emitBinaryEvent) {
  try {
    String dataString = CharStreams.toString(new InputStreamReader(data, Charsets.UTF_8));
    JSONObject xmlJSONObj = XML.toJSONObject(dataString);
    searchAndEmitEvents(xmlJSONObj.toMap(), tag, emitBinaryEvent);
  } catch (JSONException e) {
    logger.error(e.toString());
  } catch (IOException e) {
    logger.error(e.toString());
  }
}

代码示例来源:origin: com.eduworks/ew.levr.base

@Override
public Object resolve(Context c, Map<String, String[]> parameters, Map<String, InputStream> dataStreams) throws JSONException
{
  String xml = getObj(c, parameters, dataStreams).toString();
  JSONObject jsonObject = JSONML.toJSONObject(xml);
  if (optAsString("simple","true", c, parameters, dataStreams).equals("true"))
    return XML.toJSONObject(xml);
  
  return jsonObject;
}

代码示例来源:origin: com.github.lafa.tikaNoExternal/tika-external

public Metadata parse(String source) {
 JSONObject obj = XML.toJSONObject(source);
 Metadata metadata = new Metadata();
 createGrobidMetadata(source, obj, metadata);
 return metadata;
}

代码示例来源:origin: usc-isi-i2/Web-Karma

private Worksheet generateWorksheetFromXMLStream(String sourceName, InputStream is,  InputProperties inputTypeParams,
    Workspace workspace)
    throws IOException {
  Worksheet worksheet;
  String encoding = (String)inputTypeParams.get(InputProperty.ENCODING);
  int maxNumLines = (inputTypeParams.get(InputProperty.MAX_NUM_LINES) != null)? 
      (int)inputTypeParams.get(InputProperty.MAX_NUM_LINES) : -1;
      
  String contents = IOUtils.toString(is, encoding);
  JSONObject json = XML.toJSONObject(contents);
  JsonImport imp = new JsonImport(json, sourceName, workspace, encoding, maxNumLines);
  worksheet = imp.generateWorksheet();
  return worksheet;
}

相关文章