- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这种格式的 JSON:
{"Engineering":{ "Electrical Engineering":{ "ResearchStaff":["research associate","research sciencetist","senior researchsciencetist"], "Non-tenure-track":["research professaor","associateresearch professor","assistant research profesor","clinicalprofesor","clinical associate profesor","clinical assistantprofesor","visiting profesor","visiting associate profesor","visitingassistant profesor"], "Professional Staff":["businessmanager","university research administrator","departmentadministrative assistant"]},
"Computer Science":{ "Research Staff":["research associate","researchsciencetist","senior research sciencetist"], "Tenured":["distinguishedprofessor","professor","associate professor","assistant professor"],"Teaching Faculty":["distinguished professor","professor","associateprofessor","assistant professor"]},
"Computer Engineering":{"Tenured":["distinguishedprofessor","professor","associate professor","assistant professor"],"Teaching Faculty":["lecturer","senior lecturer","adjunct professor"],"Professional Staff":["business manager","university researchadministrator","department administrative assistant"]}
},
"Science":{ "Physics":{ "Research Staff":["researchassociate","research sciencetist","senior research sciencetist"],"Teaching Faculty":["lecturer","senior lecturer","adjunct professor"],"Non-Tenured-Track":["research professaor","associate researchprofessor","assistant research profesor","clinical profesor","clinicalassociate profesor","clinical assistant profesor","visitingprofesor","visiting associate profesor","visiting assistantprofesor"]},
"Chemistry":{ "Tenured":["distinguishedprofessor","professor","associate professor","assistant professor"],"Teaching Faculty":["lecturer","senior lecturer","adjunct professor"],"Non-Tenured-Track":["research professaor","associate researchprofessor","assistant research profesor","clinical profesor","clinicalassociate profesor","clinical assistant profesor","visitingprofesor","visiting associate profesor","visiting assistantprofesor"]}
}
}
这里我想绑定(bind) 4 个下拉菜单,其值如下:学院:{“工程”、“科学”}院系:选择“工程”学院时为{“电气工程”、“计算机科学”},选择“理科”学院时为{“物理”、“化学”}。
此外,在“电气工程”部门内,我们必须将{“研究人员”、“非终身教授”、“专业人员”}绑定(bind)到职位类型下拉列表中,并根据其选择,我需要绑定(bind)职位如果选择“研究人员”职位类型,则标题下拉列表为{“研究助理”、“研究科学家”、“高级研究科学家”},依此类推......
如何解析此静态 JSON 数据并将这些下拉列表分别绑定(bind)到彼此选择的值。
最佳答案
使用 XML 可能更容易做到这一点。让我为您提供使用 XML 的完整解决方案。如果您想尝试使用 JSON 解析器,则必须根据自己的需求调整此解决方案。好的,让我们开始...首先,访问这个网站...
...并将以下文件下载到名为:org.json;
我自己做了这个,并编写了一个小程序将 JSON 字符串转换为 XML:
import org.json.JSONObject;
import org.json.XML;
public class example {
public static void main(String[] args) {
JSONObject json = new JSONObject(getJSONSting() );
String xml = XML.toString(json);
System.out.println(xml);
}
private static final String getJSONSting() {
String data = "{\"Engineering\":{ \"Electrical Engineering\":{ \"Research Staff\":[\"research associate\",\"research sciencetist\",\"senior research sciencetist\"], \"Non-tenure-track\":[\"research professaor\",\"associate research professor\",\"assistant research profesor\",\"clinical profesor\",\"clinical associate profesor\",\"clinical assistant profesor\",\"visiting profesor\",\"visiting associate profesor\",\"visiting assistant profesor\"], \"Professional Staff\":[\"business manager\",\"university research administrator\",\"department administrative assistant\"]},";
data += "\"Computer Science\":{ \"Research Staff\":[\"research associate\",\"research sciencetist\",\"senior research sciencetist\"], \"Tenured\":[\"distinguished professor\",\"professor\",\"associate professor\",\"assistant professor\"], \"Teaching Faculty\":[\"distinguished professor\",\"professor\",\"associate professor\",\"assistant professor\"]}, \"Computer Engineering\":{\"Tenured\":[\"distinguished professor\",\"professor\",\"associate professor\",\"assistant professor\"], \"Teaching Faculty\":[\"lecturer\",\"senior lecturer\",\"adjunct professor\"], \"Professional Staff\":[\"business manager\",\"university research administrator\",\"department administrative assistant\"]}";
data += "},";
data += "\"Science\":{ \"Physics\":{ \"Research Staff\":[\"research associate\",\"research sciencetist\",\"senior research sciencetist\"], \"Teaching Faculty\":[\"lecturer\",\"senior lecturer\",\"adjunct professor\"], \"Non-Tenured-Track\":[\"research professaor\",\"associate research professor\",\"assistant research profesor\",\"clinical profesor\",\"clinical associate profesor\",\"clinical assistant profesor\",\"visiting profesor\",\"visiting associate profesor\",\"visiting assistant profesor\"]},";
data += "\"Chemistry\":{ \"Tenured\":[\"distinguished professor\",\"professor\",\"associate professor\",\"assistant professor\"], \"Teaching Faculty\":[\"lecturer\",\"senior lecturer\",\"adjunct professor\"], \"Non-Tenured-Track\":[\"research professaor\",\"associate research professor\",\"assistant research profesor\",\"clinical profesor\",\"clinical associate profesor\",\"clinical assistant profesor\",\"visiting profesor\",\"visiting associate profesor\",\"visiting assistant profesor\"]}";
data += "}";
data += "}";
return data;
}
}
这里是 XML 格式,您会注意到标签中非法使用空格。我的解决方案中解决了这个问题,因此您可以研究代码以了解我如何纠正问题......
<Engineering>
<Computer Science>
<Tenured>distinguished professor</Tenured>
<Tenured>professor</Tenured>
<Tenured>associate professor</Tenured>
<Tenured>assistant professor</Tenured>
<Research Staff>research associate</Research Staff>
<Research Staff>research sciencetist</Research Staff>
<Research Staff>senior research sciencetist</Research Staff>
<Teaching Faculty>distinguished professor</Teaching Faculty>
<Teaching Faculty>professor</Teaching Faculty>
<Teaching Faculty>associate professor</Teaching Faculty>
<Teaching Faculty>assistant professor</Teaching Faculty>
</Computer Science>
<Electrical Engineering>
<Professional Staff>business manager</Professional Staff>
<Professional Staff>university research administrator</Professional Staff>
<Professional Staff>department administrative assistant</Professional Staff>
<Research Staff>research associate</Research Staff>
<Research Staff>research sciencetist</Research Staff>
<Research Staff>senior research sciencetist</Research Staff>
<Non-tenure-track>research professaor</Non-tenure-track>
<Non-tenure-track>associate research professor</Non-tenure-track>
<Non-tenure-track>assistant research profesor</Non-tenure-track>
<Non-tenure-track>clinical profesor</Non-tenure-track>
<Non-tenure-track>clinical associate profesor</Non-tenure-track>
<Non-tenure-track>clinical assistant profesor</Non-tenure-track>
<Non-tenure-track>visiting profesor</Non-tenure-track>
<Non-tenure-track>visiting associate profesor</Non-tenure-track>
<Non-tenure-track>visiting assistant profesor</Non-tenure-track>
</Electrical Engineering>
<Computer Engineering>
<Tenured>distinguished professor</Tenured>
<Tenured>professor</Tenured>
<Tenured>associate professor</Tenured>
<Tenured>assistant professor</Tenured>
<Professional Staff>business manager</Professional Staff>
<Professional Staff>university research administrator</Professional Staff>
<Professional Staff>department administrative assistant</Professional Staff>
<Teaching Faculty>lecturer</Teaching Faculty>
<Teaching Faculty>senior lecturer</Teaching Faculty>
<Teaching Faculty>adjunct professor</Teaching Faculty>
</Computer Engineering>
</Engineering>
<Science>
<Chemistry>
<Tenured>distinguished professor</Tenured>
<Tenured>professor</Tenured>
<Tenured>associate professor</Tenured>
<Tenured>assistant professor</Tenured>
<Non-Tenured-Track>research professaor</Non-Tenured-Track>
<Non-Tenured-Track>associate research professor</Non-Tenured-Track>
<Non-Tenured-Track>assistant research profesor</Non-Tenured-Track>
<Non-Tenured-Track>clinical profesor</Non-Tenured-Track>
<Non-Tenured-Track>clinical associate profesor</Non-Tenured-Track>
<Non-Tenured-Track>clinical assistant profesor</Non-Tenured-Track>
<Non-Tenured-Track>visiting profesor</Non-Tenured-Track>
<Non-Tenured-Track>visiting associate profesor</Non-Tenured-Track>
<Non-Tenured-Track>visiting assistant profesor</Non-Tenured-Track>
<Teaching Faculty>lecturer</Teaching Faculty>
<Teaching Faculty>senior lecturer</Teaching Faculty>
<Teaching Faculty>adjunct professor</Teaching Faculty>
</Chemistry>
<Physics>
<Non-Tenured-Track>research professaor</Non-Tenured-Track>
<Non-Tenured-Track>associate research professor</Non-Tenured-Track>
<Non-Tenured-Track>assistant research profesor</Non-Tenured-Track>
<Non-Tenured-Track>clinical profesor</Non-Tenured-Track>
<Non-Tenured-Track>clinical associate profesor</Non-Tenured-Track>
<Non-Tenured-Track>clinical assistant profesor</Non-Tenured-Track>
<Non-Tenured-Track>visiting profesor</Non-Tenured-Track>
<Non-Tenured-Track>visiting associate profesor</Non-Tenured-Track>
<Non-Tenured-Track>visiting assistant profesor</Non-Tenured-Track>
<Research Staff>research associate</Research Staff>
<Research Staff>research sciencetist</Research Staff>
<Research Staff>senior research sciencetist</Research Staff>
<Teaching Faculty>lecturer</Teaching Faculty>
<Teaching Faculty>senior lecturer</Teaching Faculty>
<Teaching Faculty>adjunct professor</Teaching Faculty>
</Physics>
</Science>
如果您已经完成了这一步,您可以通过复制以下类来完成项目的其余部分。我已经测试了该解决方案,它似乎工作正常。
这就是看起来完成的样子...
第一个类放置在包org.combobox
中:
package org.combobox;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Node;
public class CleanNode {
private String _name;
private List<CleanNode> _nodes = new ArrayList<CleanNode>();
public CleanNode(Node node) {
_name = node.getNodeType() == Node.TEXT_NODE ? node.getNodeValue() : node.getNodeName();
}
private String getName() {
return _name;
}
public CleanNode addChild(Node node) {
CleanNode foundNode = null;
String nodeName = node.getNodeName();
for(CleanNode child : _nodes){
if(child.getName().equals(nodeName)) {
foundNode = child;
break;
}
}
if(foundNode == null) {
foundNode = new CleanNode(node);
_nodes.add(foundNode);
}
return foundNode;
}
public List<CleanNode> getNodes(){
return _nodes;
}
public String toString() {
return _name.replace("_", " ");
}
}
第二个类放置在包org.combobox
中:
package org.combobox;
import java.awt.BorderLayout;
import java.awt.Font;
import java.io.IOException;
import java.io.StringReader;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.json.JSONObject;
import org.json.XML;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class ComboBoxChain extends JFrame {
private static final long serialVersionUID = 1L;
private static String _xml;
private Node _root;
private CleanNode _cleanNode;
public ComboBoxChain() throws ParserConfigurationException, SAXException, IOException {
super("ComboBox Chain");
setUIFont (new javax.swing.plaf.FontUIResource("Monospaced",Font.PLAIN,14));
buildXML();
cleanXML();
_root = parse(_xml);
_cleanNode = new CleanNode(_root);
addCleanNodeChildren(_cleanNode, _root);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(new ComboPanel(_cleanNode), BorderLayout.CENTER);
pack();
setResizable(false);
setLocationRelativeTo(null);
}
private void addCleanNodeChildren(CleanNode cleanNode, Node node) {
if(node.getChildNodes() != null) {
NodeList children = node.getChildNodes();
for(int index = 0; index < children.getLength(); index++){
Node child = children.item(index);
CleanNode cleanChild = cleanNode.addChild(child);
addCleanNodeChildren(cleanChild, child);
}
}
}
protected Node getRootNode() {
return _root;
}
private void cleanXML() {
String xml = _xml;
int beginIndex = -1;
int endIndex = -1;
for(int index = 0; index < _xml.length(); index++) {
if(_xml.charAt(index) == '<') {
beginIndex = index;
}
else if (_xml.charAt(index) == '>') {
endIndex = index;
String originalTag = _xml.substring(beginIndex, endIndex + 1);
String newTag = originalTag;
while(newTag.contains(" ")) {
newTag = newTag.replace(" ", "_");
}
xml = xml.replace(originalTag, newTag);
}
}
_xml = xml;
}
private static final void buildXML() {
String data = "{\"Engineering\":{ \"Electrical Engineering\":{ \"Research Staff\":[\"research associate\",\"research sciencetist\",\"senior research sciencetist\"], \"Non-tenure-track\":[\"research professaor\",\"associate research professor\",\"assistant research profesor\",\"clinical profesor\",\"clinical associate profesor\",\"clinical assistant profesor\",\"visiting profesor\",\"visiting associate profesor\",\"visiting assistant profesor\"], \"Professional Staff\":[\"business manager\",\"university research administrator\",\"department administrative assistant\"]},";
data += "\"Computer Science\":{ \"Research Staff\":[\"research associate\",\"research sciencetist\",\"senior research sciencetist\"], \"Tenured\":[\"distinguished professor\",\"professor\",\"associate professor\",\"assistant professor\"], \"Teaching Faculty\":[\"distinguished professor\",\"professor\",\"associate professor\",\"assistant professor\"]}, \"Computer Engineering\":{\"Tenured\":[\"distinguished professor\",\"professor\",\"associate professor\",\"assistant professor\"], \"Teaching Faculty\":[\"lecturer\",\"senior lecturer\",\"adjunct professor\"], \"Professional Staff\":[\"business manager\",\"university research administrator\",\"department administrative assistant\"]}";
data += "},";
data += "\"Science\":{ \"Physics\":{ \"Research Staff\":[\"research associate\",\"research sciencetist\",\"senior research sciencetist\"], \"Teaching Faculty\":[\"lecturer\",\"senior lecturer\",\"adjunct professor\"], \"Non-Tenured-Track\":[\"research professaor\",\"associate research professor\",\"assistant research profesor\",\"clinical profesor\",\"clinical associate profesor\",\"clinical assistant profesor\",\"visiting profesor\",\"visiting associate profesor\",\"visiting assistant profesor\"]},";
data += "\"Chemistry\":{ \"Tenured\":[\"distinguished professor\",\"professor\",\"associate professor\",\"assistant professor\"], \"Teaching Faculty\":[\"lecturer\",\"senior lecturer\",\"adjunct professor\"], \"Non-Tenured-Track\":[\"research professaor\",\"associate research professor\",\"assistant research profesor\",\"clinical profesor\",\"clinical associate profesor\",\"clinical assistant profesor\",\"visiting profesor\",\"visiting associate profesor\",\"visiting assistant profesor\"]}";
data += "}";
data += "}";
JSONObject json = new JSONObject(data);
_xml = "<Root>" + XML.toString(json) + "</Root>";
System.out.println(_xml);
}
private static final void setUIFont (javax.swing.plaf.FontUIResource f){
Enumeration<Object> keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get (key);
if (value != null && value instanceof javax.swing.plaf.FontUIResource) {
UIManager.put (key, f);
}
}
}
private static final Node parse(String XMLContent) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(XMLContent)));
return document.getDocumentElement();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ComboBoxChain frame;
try {
frame = new ComboBoxChain();
frame.setVisible(true);
}
catch (ParserConfigurationException e) {
e.printStackTrace();
}
catch (SAXException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
第三个类放在包org.combobox
中:
package org.combobox;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ComboPanel extends JPanel {
private static final long serialVersionUID = 1L;
private static final String DEPARTMENT = "Department";
private static final String PROGRAM = "Program";
private static final String FACULTY = "Faculty";
private static final String ACADEMIC_RANK = "Academic Rank";
private JComboBox<CleanNode> _department;
private JComboBox<CleanNode> _program;
private JComboBox<CleanNode> _faculty;
private JComboBox<CleanNode> _rank;
private DependencyLink _rankLink;
private DependencyLink _facultyLink;
private DependencyLink _programLink;
private DependencyLink _departmentLink;
public ComboPanel(CleanNode root) {
super(new BorderLayout());
addCombos(root);
}
private void addCombos(CleanNode root) {
_department = new JComboBox<CleanNode>();
_program = new JComboBox<CleanNode>();
_faculty = new JComboBox<CleanNode>();
_rank = new JComboBox<CleanNode>();
_rankLink = new DependencyLink(_rank, null);
_facultyLink = new DependencyLink(_faculty, _rankLink);
_programLink = new DependencyLink(_program, _facultyLink);
_departmentLink = new DependencyLink(_department, _programLink);
_departmentLink.populate(root);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 2));
panel.add(new JLabel(" " + DEPARTMENT + " : "));
panel.add(_department);
panel.add(new JLabel(" " + PROGRAM + " : "));
panel.add(_program);
panel.add(new JLabel(" " + FACULTY + " : "));
panel.add(_faculty);
panel.add(new JLabel(" " + ACADEMIC_RANK + " : "));
panel.add(_rank);
add(panel, BorderLayout.CENTER);
}
}
第四个也是最后一个类位于包org.combobox
中:
package org.combobox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JComboBox;
public class DependencyLink implements ActionListener {
private boolean _isLocked;
private JComboBox<CleanNode>_combo;
private DependencyLink _childLink;
private CleanNode _previousNode;
public DependencyLink(JComboBox<CleanNode>combo, DependencyLink childLink) {
_combo = combo;
_combo.addActionListener(this);
_childLink = childLink;
}
public void actionPerformed(ActionEvent e) {
CleanNode node = (CleanNode)_combo.getSelectedItem();
if(node != _previousNode) {
updateChild();
_previousNode = node;
}
}
private void updateChild() {
if( _childLink != null) {
if(!_isLocked) {
_childLink.populate((CleanNode)_combo.getSelectedItem());
}
}
}
public void populate(CleanNode parentNode) {
_isLocked = true;
_combo.removeAllItems();
if(parentNode != null) {
List<CleanNode> children = parentNode.getNodes();
if(!children.isEmpty()) {
for(CleanNode childNode : children) {
_combo.addItem(childNode);
}
_combo.setSelectedItem(children.get(0));
}
}
_isLocked = false;
updateChild();
}
}
研究代码并根据自己的需要进行调整以实现您自己的实现。
祝你好运,康斯坦丁
关于java - 如何解析 JSON 数据以绑定(bind)依赖下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31372818/
初学者 android 问题。好的,我已经成功写入文件。例如。 //获取文件名 String filename = getResources().getString(R.string.filename
我已经将相同的图像保存到/data/data/mypackage/img/中,现在我想显示这个全屏,我曾尝试使用 ACTION_VIEW 来显示 android 标准程序,但它不是从/data/dat
我正在使用Xcode 9,Swift 4。 我正在尝试使用以下代码从URL在ImageView中显示图像: func getImageFromUrl(sourceUrl: String) -> UII
我的 Ubuntu 安装 genymotion 有问题。主要是我无法调试我的数据库,因为通过 eclipse 中的 DBMS 和 shell 中的 adb 我无法查看/data/文件夹的内容。没有显示
我正在尝试用 PHP 发布一些 JSON 数据。但是出了点问题。 这是我的 html -- {% for x in sets %}
我观察到两种方法的结果不同。为什么是这样?我知道 lm 上发生了什么,但无法弄清楚 tslm 上发生了什么。 > library(forecast) > set.seed(2) > tts lm(t
我不确定为什么会这样!我有一个由 spring data elasticsearch 和 spring data jpa 使用的类,但是当我尝试运行我的应用程序时出现错误。 Error creatin
在 this vega 图表,如果我下载并转换 flare-dependencies.json使用以下 jq 到 csv命令, jq -r '(map(keys) | add | unique) as
我正在提交一个项目,我必须在其中创建一个带有表的 mysql 数据库。一切都在我这边进行,所以我只想检查如何将我所有的压缩文件发送给使用不同计算机的人。基本上,我如何为另一台计算机创建我的数据库文件,
我有一个应用程序可以将文本文件写入内部存储。我想仔细看看我的电脑。 我运行了 Toast.makeText 来显示路径,它说:/数据/数据/我的包 但是当我转到 Android Studio 的 An
我喜欢使用 Genymotion 模拟器以如此出色的速度加载 Android。它有非常好的速度,但仍然有一些不稳定的性能。 如何从 Eclipse 中的文件资源管理器访问 Genymotion 模拟器
我需要更改 Silverlight 中文本框的格式。数据通过 MVVM 绑定(bind)。 例如,有一个 int 属性,我将 1 添加到 setter 中的值并调用 OnPropertyChanged
我想向 Youtube Data API 提出请求,但我不需要访问任何用户信息。我只想浏览公共(public)视频并根据搜索词显示视频。 我可以在未经授权的情况下这样做吗? 最佳答案 YouTube
我已经设置了一个 Twilio 应用程序,我想向人们发送更新,但我不想回复单个文本。我只是想让他们在有问题时打电话。我一切正常,但我想在发送文本时显示传入文本,以确保我不会错过任何问题。我正在使用 p
我有一个带有表单的网站(目前它是纯 HTML,但我们正在切换到 JQuery)。流程是这样的: 接受用户的输入 --- 5 个整数 通过 REST 调用网络服务 在服务器端运行一些计算...并生成一个
假设我们有一个名为 configuration.js 的文件,当我们查看内部时,我们会看到: 'use strict'; var profile = { "project": "%Projec
这部分是对 Previous Question 的扩展我的: 我现在可以从我的 CI Controller 成功返回 JSON 数据,它返回: {"results":[{"id":"1","Sourc
有什么有效的方法可以删除 ios 中 CBL 的所有文档存储?我对此有疑问,或者,如果有人知道如何从本质上使该应用程序像刚刚安装一样,那也会非常有帮助。我们正在努力确保我们的注销实际上将应用程序设置为
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误 注意:数据是从 Azure 中获取的 spec.js const accou
我是一名优秀的程序员,十分优秀!