使用java解析和排序JSON数据

时间:2016-08-14 10:14:17

标签: java json sorting

我希望使用org.json.simple库解析以下数组并遇到困难。有人可以看看我的json文件和代码,并告知我做错了什么

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:saxon="http://saxon.sf.net/"
    xmlns:dtd="http://saxon.sf.net/dtd"
    xmlns:axsl="http://www.w3.org/1999/XSL/Transform-Alias"
    exclude-result-prefixes="xs saxon dtd axsl"
    extension-element-prefixes="saxon"
    version="2.0">

    <xsl:param name="sheet-uri" as="xs:string" select="'test201608140301.xsl'"/>

    <xsl:param name="sheet" select="doc($sheet-uri)"/>

    <xsl:param name="sheet-id" as="xs:string" select="'sheet'"/>

    <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>

    <xsl:template match="/*">
        <saxon:doctype>
            <dtd:doctype name="{name()}">
                <dtd:element name="{name()}" content="ANY"/>
                <dtd:element name="{name($sheet/*)}" content="ANY"/>
                <dtd:attlist element="{name($sheet/*)}">
                    <dtd:attribute name="id" type="ID" value="#REQUIRED"/>
                </dtd:attlist>
            </dtd:doctype>
        </saxon:doctype>
        <xsl:processing-instruction name="xml-stylesheet">type="text/xsl" href="#<xsl:value-of select="$sheet-id"/>"</xsl:processing-instruction>
        <xsl:copy>
            <xsl:apply-templates select="$sheet/node()" mode="include-sheet"/>
            <xsl:copy-of select="node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/*" mode="include-sheet">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:attribute name="id" select="$sheet-id"/>
            <axsl:template match="{name()}"/>
            <xsl:copy-of select="node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

请注意,一些EmpCode字段也是空白的。我应该怎么处理它们。此外,我需要能够在显示数据时重新排列数据,我的意思是我需要根据EmpCode或Type对数据进行排序。

我编写的用于解析上述json的代码如下所述:

{
  "Company": [
{
  "Department": "Engineering",
  "Employee": [
    {
      "EmpName": "Jack",
      "EmpCode": "8",
      "Type": "Permanent"
    }, {
      "EmpName": "John",
      "EmpCode": "45",
      "Type": "Permanent"
    }, {
      "EmpName": "Ron",
      "EmpCode": "9",
      "Type": "Contract"
    }, {
      "EmpName": "Jin",
      "EmpCode": "6",
      "Type": "Permanent"
    }, {
      "EmpName": "Jill",
      "EmpCode": "",
      "Type": "Retired"
    }, {
      "EmpName": "Sam",
      "EmpCode": "89",
      "Type": "Permanent"
    }, {
      "EmpName": "Jonathan",
      "EmpCode": "66",
      "Type": "Permanent"
    }, {
      "EmpName": "Craig",
      "EmpCode": "",
      "Type": "Ex-Employee"
    }, {
      "EmpName": "Son Hui",
      "EmpCode": "4",
      "Type": "Permanent"
    }, {
      "EmpName": "Joshua",
      "EmpCode": "12",
      "Type": "Contract"
    }, {
      "EmpName": "Tulip",
      "EmpCode": "70",
      "Type": "Contract"
    }
  ]
}, {
  "Department": "IT",
  "Employee": [
    {
      "EmpName": "Nico",
      "EmpCode": "50",
      "Type": "Resigned"
    }, {
      "EmpName": "Phil",
      "EmpCode": "103",
      "Type": "Resigned"
    }
  ]
}
  ]
}

1 个答案:

答案 0 :(得分:1)

看起来你误解了Json结构的工作原理以及它可能包含的对象类型。

例如,您认为您在这段代码中做了什么?

JSONArray slideContent = (JSONArray) jsonObject.get("Department");
Iterator i = slideContent.iterator();

看起来你试图获取名为“Department”的json对象,但它不是JsonArray,它是一个String。

"Department": "Engineering"
//Writing code "jsonObject.get("Department")" 
//you will get string "Engineering"

//this code will be correct
(JSONArray) jsonObject.get("Employee")
//because a json value for name "Employee" it's an JsonArray