Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 递归方法返回存储为JSON文件的不同对象_Java_Json_Recursion - Fatal编程技术网

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

Java 递归方法返回存储为JSON文件的不同对象,java,json,recursion,Java,Json,Recursion,我找到了这个答案,我认为它是适用的,但有点扭曲 我有两个使用YML子集的JSON格式的文件,其中一个是设备列表,另一个是列出特定类型设备属性的文件 选择将设备实例列表划分为一个文件和另一个文件中的属性是为了允许设备制造商更改属性,而无需返回并重写/重新编译硬编码属性 在任何情况下,我都可以使用对JSONParser的两个不同调用,然后在最后将属性列表添加到设备对象,但这种解决方案似乎是在浪费代码,因为除了设置值的while循环的内部之外,它们做的事情完全相同 我认为在内部循环中,类似Ruby的

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

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

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

在任何情况下,我都可以使用对JSONParser的两个不同调用,然后在最后将属性列表添加到设备对象,但这种解决方案似乎是在浪费代码,因为除了设置值的while循环的内部之外,它们做的事情完全相同

我认为在内部循环中,类似Ruby的收益率可能会起作用,但不确定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}}
在每个设备文件中只有一个映射,尽管它位于列表中。处理逻辑是打开文件,创建解析器,读取json,并为列表中的每个条目处理映射

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

一种方法是使用回调:创建一个接口
MapHandler
,该接口具有一个方法
process
openJSON
获取此类型的参数,并为每个方法对其调用
process


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

此外,可能循环的内部部分可以作为匿名函数传递,或者可能可以使用装饰器模式将函数添加到LoadJSON。因此,这两个文件具有完全相同的结构?不,这两个文件具有不同的结构。有一个设备的主列表,每个设备都有一个类型。每种类型都有一个类型文件,包含属性。很抱歉,我的意思是,除了名称之外,它们具有相同的结构,在这两种情况下都有一个映射列表。创建一个名为Loadable的超类,然后让两种类型的对象从同一个类派生,并重载适当的函数,怎么样?