0

I want to Read a file and store it in an array, then split up the array everywhere there is a }, so that each item in the array contains the string split up. Here is what I mean,

My file contains stuff like this:

[
{
    "sample1": {
        "color": "red", 
        "date": "2011, 
        "name": "george"
    }
}, 
{
    "sample2": {
        "color": "red", 
        "date": "2012", 
        "name": "phil"
    }
}, 

I want to read this file, have have for example the oth element in the array to represent

{
        "sample1": {
            "color": "red", 
            "date": "2011, 
            "name": "george"
        }
}, 

and for element number one to represent

    {
    "sample2": {
        "color": "red", 
        "date": "2012", 
        "name": "phil"
    }
}, 

Now I am not too sure how to do this, I don't know what the size of my array will be and I am not sure how I will access each element of my array, I have started off with this so far

String str;
  char[] getArray;

  FileReader fr = new FileReader("/sampleProject");
  BufferedReader br = new BufferedReader(fr);

  br.readLine();

  while ((str = br.readLine()) != null) {
     str.toCharArray();
     str.split("},");

  }

  System.out.println("Here it is" + str);

  br.close();
}
4

3 回答 3

0

您最好使用 Jackson Mapper 将该 JSON 解析为一个对象,如下所示:

        import org.codehaus.jackson.JsonGenerationException;
        import org.codehaus.jackson.map.JsonMappingException;
        import org.codehaus.jackson.map.ObjectMapper;

        public void readJSON(){
            MyBean bean;
            ObjectMapper mapper = new ObjectMapper();

            try {
                bean = mapper.readValue(new File("/sampleProject"), MyBean.class);

            } catch (JsonGenerationException e) {
                  e.printStackTrace();
            } catch (JsonMappingException e) {
                  e.printStackTrace();
            } catch (IOException e) {
                  e.printStackTrace();
            }
        }

您可以在以下位置获取有关 JSON -> Java 对象转换的更多信息:http ://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

于 2013-09-10T14:35:04.880 回答
0

如果您不知道数组的大小,您可以使用 ArrayList。 str.toCharArray();并且str.split("},")};可能不会做您期望的事情,因为您从未分配返回值。

我没有对此进行测试,但也许它可以满足您的需要:

String str;
  char[] getArray;

  FileReader fr = new FileReader("/sampleProject");
  BufferedReader br = new BufferedReader(fr);

  br.readLine();
  ArrayList<String> result = new ArrayList<>();

  while ((str = br.readLine()) != null) {
     String[] array = str.split("},");
     for(int i = 0; i< array.length;i++) {
         result.add(array[i]);
     }
  }

  System.out.println("Here it is" + str);

  br.close();
于 2013-09-10T14:40:17.227 回答
0

你已经很接近它了。只是一个视图的东西,没有必要。如果数据足够小以将其完全保存在内存中,这将是您的代码:

StringBuilder data = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader("/sampleProject"));

// Read data in
String line;
while ((line = br.readLine()) != null) data.append(line);

// Split into packages
for(String package : data.toString().split("}")
    System.out.println("Here it is" + package + "}");

br.close();

一件小事是,split删除了}您可以在System.println().

是的,它看起来像 JSON 存在良好的 JSON 解析器,例如这里。但如果你不需要它...

当您的数据变得太大而无法保存在内存中时,可能会出现更相关的情况。您需要设置一个完全流式传输的解决方案。

于 2013-09-10T14:41:38.367 回答