java - 递归方法返回存储为 JSON 文件的不同对象

标签 java json recursion

我找到了这个答案,我认为它适用,但有一点扭曲。

How to return multiple objects from a Java method?

我有两个使用 YML 子集的 JSON 格式文件,其中一个是设备列表,另一个是列出特定类型设备的属性的文件。

选择将设备实例列表划分为一个文件并将属性划分为另一个文件是为了允许设备制造商更改属性,而无需返回并重写/重新编译硬编码属性。

无论如何,我可以对 JSONParser 使用两个不同的调用,然后最后将属性列表添加到 Device 对象,但该解决方案似乎浪费代码,因为除了 while 的内部部分之外循环设置值,它们执行完全相同的操作。

我认为像 Ruby-ish Yield 这样的东西可能会在内部循环中发挥作用,但不确定 Java 中是否存在。

所以,不用多说,代码如下:

// Import the json simple parser, used to read configuration files
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.*;

public  class LoadJson {

    private String path = "";
    private String fileType = "";

    //public LoadJson(String path, String fileType){

        //this.path = path;
        //this.fileType = fileType;

    //}

    public static Device openJSON(String fileType, String deviceName) {
        JSONParser parser = new JSONParser();
        Device myDevice = new Device();

        ContainerFactory containerFactory = new ContainerFactory(){
            public List creatArrayContainer() {
              return new LinkedList();
            }

        public Map createObjectContainer() {
          return new LinkedHashMap();
        }

      };

      try {
            File appBase = new File("."); //current directory
            String path = appBase.getAbsolutePath();
            System.out.println(path);

            Map obj = (Map)parser.parse(new FileReader(path+fileType),containerFactory);

            Iterator iter = obj.entrySet().iterator();

            //Iterator iterInner = new Iterator();

            while(iter.hasNext()){
                //LinkedList entry = (LinkedList)iter.next();
                LinkedList myList = new LinkedList(); 
                Map.Entry entry = (Map.Entry)iter.next();

                myList = (LinkedList) (entry.getValue());

                Iterator iterate = myList.iterator();
                while (iterate.hasNext())
                {
                    LinkedHashMap entry2 = (LinkedHashMap)iterate.next();
                    if(fileType=="mav2opc")
                            {
                             String deviceName1 = entry2.get("DeviceName").toString();
                             String userName = entry2.get("UserName").toString();
                             String password = entry2.get("Password").toString();
                             String deviceType = entry2.get("DeviceType").toString();
                             String ipAddress = entry2.get("IPAddress").toString();
                             myDevice = new Device(deviceName1, userName, password, deviceType,ipAddress);
                             openJSON(deviceType,deviceName1);
                             System.out.println(myDevice);
                            } else
                            {
                                //Add a tag
                                String tagName = entry2.get("tagName").toString();
                                String tagType = entry2.get("tagType").toString();
                                String tagXPath = entry2.get("tagXPath").toString();
                                String tagWritable = entry2.get("tagWritable").toString();
                            }

                }
            } 

            //System.out.println("==toJSONString()==");
            //System.out.println(JSONValue.toJSONString(json));

      } catch (FileNotFoundException e) {
            e.printStackTrace();
      } catch (IOException e) {
            e.printStackTrace();
      } catch(ParseException pe){
            System.out.println(pe);
    }
      return myDevice;
}

}

最佳答案

好的,您有两个文件,一个包含设备列表,另一个是每个设备的文件,其中包含设备的属性。这两个文件的结构完全相同,我猜是这样的

设备文件:

{{"DeviceName:"d1","UserName":"u1","Password":"p1","DeviceType":"t1","IPAddress":"i1"},
 {"DeviceName:"d2","UserName":"u2","Password":"p2","DeviceType":"t2","IPAddress":"ir"}}

以及每个设备的文件

{{"tagName:"n1","tagType":"t1","tagXPath":"X1","tagWritable":true}}

在每个设备文件中只有一个 map ,尽管它位于列表中。处理逻辑是打开文件、创建解析器、读取 json 并为列表中的每个条目处理映射。

处理 map 的逻辑是两者之间的唯一区别。请注意,现在您将丢弃从每个设备文件中读取的内容,您必须将其存储在 myDevice

中的某个位置

实现此目的的一种方法是使用回调:创建一个具有方法 process 的接口(interface) MapHandleropenJSON 采用此类型的参数,并为每个方法调用 process

可以使用正在处理的 myDevice 来构造设备级处理程序并设置字段。

关于java - 递归方法返回存储为 JSON 文件的不同对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9399705/

相关文章:

javascript - Ajax无法加载php json

javascript - 如何在父子层次结构中显示表格?

java - fragment 布局问题

java - 启动已停止的线程时出现运行时异常,但恢复时没有异常

java - 以编程方式将 HttpSessionListener 添加到 Jetty 不起作用

java - google-httpclient JSON 定义

java - 如何调用Akka helloworld?

android - 通过gson解析json返回null

scala - 为什么 Scala 'match' 卡在一个永远的循环中

java - 缺少返回语句错误