0

I have a string array like string[] sentences which consist of sentences one in each index like This is the first message in sentences[0] and This is the second message in sentences[1] and so on. My java code to send the information to the server for sentiment analysis is like this:

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());  
        out.write(
                "[ " +
                "\"Can't wait for the movie\"," +
                "\"Countdown! Only ten days remaining!\" " +
                "]");
        out.flush();
        out.close();

How do I replace the texts above in the code by the string array for it's length say n?

2
  • How do I add an array of string into the following JSON?, where is the JSON? this question has nothing to do with JSON. Commented Feb 7, 2016 at 7:05
  • Got it, sorry. Anyway, how do I convert this array of string into JSON array? Commented Feb 7, 2016 at 7:11

2 Answers 2

12

Use Gson library, which convert Java object to Json String

    String[] sentences=new String[3];
    sentences[0]="Hi";
    sentences[1]="Hello";
    sentences[2]="How r u?";

    Gson gson=new GsonBuilder().create();
    String jsonArray=gson.toJson(sentences);

    //["Hi","Hello","How r u?"]

    out.write(jsonArray);
    out.flush();
    out.close();
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, I tried that but then I got 200: bad JSON format error as the response. I tried this but I'm not sure whether it's right. It responds with neutral to all. OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write( "[ " + "\"+tweets[0]+\"," + "\"+tweets[1]+\" " + "]"); out.flush(); out.close(); What could be the issue?
0

The easiest solution is a loop:

StringBuilder sb = new StringBuilder("[");
for(int i = 0; i < array.length; i++) {
    sb.append(array[i]);
    if(i < array.length-1) {
        sb.append(",");
    }
}
out.write(sb.append("]").toString());

But this has the problem of producing potentially invalid JSON (unescaped). Hence:

The best solution, however, would be to use a proper JSON/Java binding library such as Jackson, Gson, etc.

3 Comments

Hey, I tried that but then I got 200: bad JSON format error as the response. I tried this but I'm not sure whether it's right. It responds with neutral to all. OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write( "[ " + "\"+tweets[0]+\"," + "\"+tweets[1]+\" " + "]"); out.flush(); out.close(); What could be the issue?
As I pointed out, this can produce invalid json. That's why you're highly encouraged to use Jackson or Gson.
StringBuilder sb=new StringBuilder(); sb.append("[ \""); for(int x=0;x<val;x++) { String a=xyz[x].replaceAll("[^A-Za-z0-9]"," "); sb.append("+"); sb.append(a); if(x < val-1) { sb.append("+\",\""); } } sb.append("+\" ]"); System.out.println(sb.toString()); out.write(sb.toString()); This worked perfectly for me. Thanks a lot!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.