0

I am trying to convert CSV into JSON and using OpenCSV and trying to add attributes of JSON from CSV header. For that, I am extracting first row from CSV into separate array and then processing the remaining CSV and adding it to JSON. But it's not writing in JSON in the same sequence as that of CSV.

For Example: CSV

name,address,phone
john,palo alto, 999
michael, SFO, 4342

And the JSON i get is:

[
  {
    "address" : palo alto,
    "name" : john,
    "phone" : 999
  },
  {
    "address" : SFO,
    "name" : michael,
    "phone" : 4342
  },
  ......
  ......
]

Here is my java code:

// Iterate through the rows.
    CSVReader reader = null;
    try {
        fr = new FileReader(csvFile);

        // create a new CSV reader
        reader = new CSVReader(fr, ',', '"', 0);

        // Read all rows at once
        List<String[]> allRows = reader.readAll();

        // store the header field from CSV to be used to define properties
        // like name, address, contact etc
        String[] header = allRows.get(0);

        // Read CSV line by line and use the string array as you want
        for (int j = 1; j < allRows.size(); j++) {

            String[] row = allRows.get(j);

            JSONObject cells = new JSONObject();

            int i = 0;
            for (; i < row.length; i++) {
                String cellValue = row[i];
                cells.put(header[i], cellValue);

            }               

            //write to JSON array
            json.put(cells);
        }


    } catch (FileNotFoundException e) {


    } catch (IOException e) {


    } catch (JSONException e) {


    } finally {
        try {
            reader.close();
            fr.close();
        } catch (IOException e) {

        }
    }

UPDATE: I am displaying the JSON data over UI in table using javascript. JSON data is returned as the response. Have to display in particular table order.

roger_that
  • 9,493
  • 18
  • 66
  • 102
  • 2
    Possible duplicate of [JSON order mixed up](http://stackoverflow.com/questions/3948206/json-order-mixed-up) – dnault Oct 11 '16 at 22:30
  • 1
    it jsut looks like it's sorting it alphabetically perhaps. also why would order matter for JSON attributes? – RAZ_Muh_Taz Oct 11 '16 at 22:42
  • It appears to be alphabetizing the JSON properties... does it matter if they're not in the same order as long as the property correlates with the correct value? The JSON has effectively turned the rows in your CSV into objects so can't you just access them as you would any object in whatever code you're using to consume the JSON? How are you using the JSON after the conversion? – big_water Oct 11 '16 at 22:42
  • Yeah. I understand. Order does not matter here as the properties are correlating with correct values. I was just curious to know the behavior. Can I sort the JSON string based on properties? – roger_that Oct 11 '16 at 22:44
  • @roger_that, how are you consuming/using your JSON after you create it? we're trying to understand the purpose of sorting the properties. – big_water Oct 11 '16 at 22:46
  • @big_water updated the question. Please see. – roger_that Oct 11 '16 at 23:00
  • 1
    @roger_that, you should write the UI code in a way that will format the table in the correct order regardless of how it's returned from the JSON conversion library server-side. If you need help with this, please post your UI code then we can assist! – big_water Oct 11 '16 at 23:06

0 Answers0