1

I have a situation where my original data have some comma. Actually that data is JSON object which contains some comma

I am reading a file which contain my JSON object by BufferedReader Now that BufferedReader output I am adding in list.

Everything is working fine but the request get failed because .toString(); added additional comma of list too

String sCurrentLine = null;
BufferedReader br = new BufferedReader(new FileReader("/home/shubham/git/productApi"));
ArrayList<String> list = new ArrayList<String>();

while ((sCurrentLine = br.readLine()) != null) {
    System.out.println(sCurrentLine);
    list.add(sCurrentLine);
}

request.payload = list.toString();
System.out.println("dd =" + request.payload);

My JSON

{
    "products":
    {
        "productsApp11": {
            "code": "productsApp11",
            "name": "productsApp11",
            "attribute_set": "one",
            "product_type": "product",
            "status": "active"
            }
    }
}

But because of .toString data is coming like below:-

[{,
    "products": ,
    {,
        "productsApp11": {,
            "code": "productsApp11",
            ,
            "name": "productsApp11",
            ,
            "attribute_set": "Apparel",
            ,
            "product_type": "product",
            ,
            "status": "active",
        },
    },
}]

Any solution would be appreciated

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • 1
    Instead of using `BufferedReader`, try using the `Scanner` class and providing a `","` delimiter. – Mr. Polywhirl Mar 18 '16 at 13:55
  • 2
    why are you adding the file lines to the list? why not simply store them in a `String`? – Tommaso Bertoni Mar 18 '16 at 13:56
  • @Mr.Polywhirl -> can you please provide any example. – Shubham Jain Mar 18 '16 at 13:58
  • Also, don't forget to close your buffered reader! `br.close()` Or you will leak resources. – Mr. Polywhirl Mar 18 '16 at 13:58
  • @TommyTopes -> Do you mean string array? – Shubham Jain Mar 18 '16 at 13:58
  • 1
    What does your data look like? Is there a mock data sample you can post? – Mr. Polywhirl Mar 18 '16 at 13:59
  • @Mr.Polywhirl -> Thanks to point it out .. Done with br.close() – Shubham Jain Mar 18 '16 at 14:00
  • @Mr.Polywhirl -> Added my JSON object – Shubham Jain Mar 18 '16 at 14:01
  • @ShubhamJain No, I mean String. I think the "additional commas" come from the fact that when he calls `list.toString()` `ArrayList` adds a comma for each element stored in it. If he stored `sCurrentLine` in a String instead (`myData += sCurrentLine`) and then write `request.payload = myData` there wouldn't be that problem – Tommaso Bertoni Mar 18 '16 at 14:03
  • I believe that your should read the entirety of the file into a String and use Jackson or Gson to convert that data into a `Map`. It is an object structure, use is as such. Check out: [Convert Json to Map](http://stackoverflow.com/questions/443499/convert-json-to-map). – Mr. Polywhirl Mar 18 '16 at 14:05
  • What is `request.payload` used for? @TommyTopas I would suggest using a `StringBuilder` instead of a `String`, but you are right. – Alexander Mar 18 '16 at 14:10
  • Yes @Alexander I agree. But actually I think it would be simplier to read the entire file, print it and set it to the payload ([`Files.readAllLines`](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllLines%28java.nio.file.Path,%20java.nio.charset.Charset%29)). – Tommaso Bertoni Mar 18 '16 at 14:13

1 Answers1

2

You need to join your list using line seperators. The dafault toString() for a List delimits items by a comma.

Java 8

String sCurrentLine = null;
BufferedReader br = new BufferedReader(new FileReader("/home/shubham/git/productApi"));
ArrayList<String> list = new ArrayList<String>();

while ((sCurrentLine = br.readLine()) != null) {
    list.add(sCurrentLine);
}

br.close();

request.payload = String.join(System.getProperty("line.separator"), list);
System.out.println("dd =" + request.payload);

Guava

Joiner.on(System.getProperty("line.separator")).join(list);

Java 7

String payload = join(System.getProperty("line.separator"), list);
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) {
    StringBuffer buff = new StringBuffer();

    if (elements != null) {
        Iterator<?> it = elements.iterator();

        if (it.hasNext()) {
            buff.append(it.next());
        }

        while (it.hasNext()) {
            buff.append(delimiter).append(it.next());
        }
    }

    return buff.toString();
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132