1

I have a csv file

 input.csv
    1,[103.85,1.28992],[103.89,1.294],[103.83,1.216]
    2,[103.5,1.292],[103.9,1.4],[103.3,1.21]
    3,[103.6,1.291],[103.6,1.39],[103.3,1.29]

From this I need to convert it into

{

                    "type": "LineString",
                    "coordinates": [[103.85,1.28992],[103.89,1.294],[103.83,1.216]]


                "properties": {
                    "id": "1"

                }
            },
            {

                "type": "LineString",
                "properties": {
                    "id": "2"

                },
                "coordinates": [[103.5,1.292],[103.9,1.4],[103.3,1.21]]



        },{

                "type": "LineString",
                "properties": {
                    "id": "3"

                },
                "coordinates": [[103.6,1.291],[103.6,1.39],[103.3,1.29]]



        }

I am now trying to do it in java.So I read the csv file with open csv

try (CSVReader reader = new CSVReader(new FileReader(fileName))) {
            String[] nextLine;

            while ((nextLine = reader.readNext()) != null) {

                for (String e: nextLine) {
                   // System.out.format("%s ", e);
                System.out.println( e.split(",",1));
                }
            }

But I am having problem while spliting the line.If you look at the first line then I want to have 1 as a part and the rest [103.85,1.28992],[103.89,1.294],[103.83,1.216] as another part.So that I can build the String

  String s="{\"type\": \"LineString\", \"coordinates\": "+s[1]+"
     \"properties\": { \"id\":"+s[0]+"} }";

Any help is appreciated

1
  • @Jens i do not read a json file I read a csv and write a JSON file Commented Mar 6, 2017 at 8:14

4 Answers 4

1

You can try that:

(\d+),(.*)

You don't need to split... if you execute it you get two group . Group 1 is the digit and Group 2 is the later contents Explanation

Try this Sample:

final String regex = "(\\d+),(.*)";
final String string = "1,[103.85,1.28992],[103.89,1.294],[103.83,1.216]\n"
     + "2,[103.5,1.292],[103.9,1.4],[103.3,1.21]\n"
     + "3,[103.6,1.291],[103.6,1.39],[103.3,1.29]";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is this the parameter to split spilit.("(\d+),(.*)")?
Thank you for the explanation as well :)
0

Use JSONSimple to create the JSON you need. IN my opinion the simplest JSON lib. See this example of use.

Comments

0

You can parse the lines yourself:

try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
    String nextLine;
    while ((nextLine = reader.readLine()) != null) {
        int ix = nextLine.indexOf(',');
        if (ix >= 0) {
            String head = nextLine.substring(0, ix);
            String tail = nextLine.substring(ix+1);
            doSomethingWith(head, tail);
        }
    }
}

2 Comments

Cannot invoke indexOf(char) on the array type String[] error throws for this
@Josh it's a String, not a String[]
0

The problem is that to get the data the way you need to have whatever is generating the input.csv file to encapsulate the different sections in quotes.

So either

input.csv
   1,"[103.85,1.28992],[103.89,1.294],[103.83,1.216]"
   2,"[103.5,1.292],[103.9,1.4],[103.3,1.21]"
   3,"[103.6,1.291],[103.6,1.39],[103.3,1.29]"

Or

input.csv
   "1","[103.85,1.28992],[103.89,1.294],[103.83,1.216]"
   "2","[103.5,1.292],[103.9,1.4],[103.3,1.21]"
   "3","[103.6,1.291],[103.6,1.39],[103.3,1.29]"

Because as it is between the one and the end of the line there are six commas which any csv parser will interpret that the line has seven columns instead of two.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.