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();
}
Jack
  • 343
  • 2
  • 3
  • 5

3 Answers3

0

You'd better parse that JSON into an Object using Jackson Mapper, like this:

        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();
            }
        }

You can get more information about JSON -> Java Objects conversion in: http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

Amin Abu-Taleb
  • 4,423
  • 6
  • 33
  • 50
0

If you don't know, what size an array is you can use ArrayList. str.toCharArray(); and str.split("},")}; probably don't do what you expect, since you never asign the returned value.

I didn't test this, but maybe it does what you need:

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();
Kayz
  • 637
  • 1
  • 8
  • 21
  • Why do you copy the result of `split` into an array list? It is already an array of strings that can be accessed directly. – jboi Sep 10 '13 at 14:43
  • `array` is a new array for each line in the file. So if you don't copy it, in the end you will only have the last line in your array. – Kayz Sep 10 '13 at 15:58
  • Good point, but in this solution you print out `str` which is only the last line anyway. @Jack, @Kayz: I still still think this one will give you a misleading result. That's why I use a `StringBuilder` and `append` line by line to get the real full package. – jboi Sep 10 '13 at 16:10
  • oh, you're right. I totally forgot about the output in the end. – Kayz Sep 10 '13 at 16:58
0

You got quite close to it. Just a view things, that are not necessary. If the data is small enough to keep it fully in memory, this would be your code:

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();

One minor thing is, that the split removes the final } which you could append again in the System.println().

And yes, it looks like JSON where good JSON Parser exist, e.g. here. But if you dont need it...

More relevant is maybe the case, when your data gets too big to keep in memory. You would need to setup a fully streamed solution.

jboi
  • 11,324
  • 4
  • 36
  • 43