V
V
Vadim2014-10-11 15:51:16
Java
Vadim, 2014-10-11 15:51:16

How to display a string in Java containing special. characters?

I use Talend ESB to upload the price list from the program (Firebird database) to the site. in the site database (MySQL) in one field there should be a line like

'{\n "metadata.title": "",\n "metadata.description": "",\n "metadata.keywords": "",\n "metadata.robots": "",\n "metadata .author": "",\n "config.enable_comments": 1,\n "config.primary_category": 205,\n "jbzoo.no_index": 0\n}'
' , where else do I need to insert my values ​​(row1.DESCR) from Firebird,
let's say
"metadata.description": "row1.DESCR"
from request.
I don’t understand Java and I don’t know the syntax, Talend returns errors "Syntax error in token" and "Syntax error; insert ";" to end Statement" /
how to display everything in one line starting with one. quote and containing quotes and special characters ,

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
FanKiLL, 2014-10-11
@voznik

That is, in essence, you need json. Why do you create it with your hands and suffer with shielding.
When you can make a java class with the fields you need, initialize it with the necessary parameters and get a json string from it, which you can send wherever you need.
Use Google's gson library - https://code.google.com/p/google-gson/
For example:
Let's make a class:

public class TalentESBData {

    @SerializedName("metadata.title")
    private String metadataTitle;
    @SerializedName("metadata.description")
    private String metadataDescription;
    @SerializedName("metadata.keywords")
    private String metadataKeywords;
    @SerializedName("metadata.robots")
    private String metadataRobots;
    @SerializedName("metadata.author")
    private String metadataAuthor;
    @SerializedName("config.enable_comments")
    private int configEnableComments;
    @SerializedName("config.primary_category")
    private int configPrimaryCategory;
    @SerializedName("jbzoo.no_index")
    private int jbzooNoIndex;

    // Геттеры и сеттеры опустим их любая ide сгенерирует...

    public String toJson(){
        Gson gson = new Gson(); //библиотека для генерации json
        String jsonResponse = gson.toJson(this);
        return jsonResponse;
    }
}

And now how to use it.
public class TalentMain {

    public static void main(String[] args) {

        TalentESBData talentData = new TalentESBData(); //инициализируем класс
        talentData.setMetadataDescription(""); //заполняем поля какие надо
        talentData.setMetadataAuthor("");
        talentData.setMetadataKeywords("");
        talentData.setMetadataRobots("");
        talentData.setMetadataTitle("");
        talentData.setConfigEnableComments(1);
        talentData.setConfigPrimaryCategory(205);
        talentData.setJbzooNoIndex(0);


        System.out.println(talentData.toJson()); // Получаем json и выводим в консоль, в вашем случа
        // вместо консоли можете отправлять эти данные или что там нужно сними делать.
    }
}

At the output we have such json,
{
  "metadata.title": "",
  "metadata.description": "",
  "metadata.keywords": "",
  "metadata.robots": "",
  "metadata.author": "",
  "config.enable_comments": 1,
  "config.primary_category": 205,
  "jbzoo.no_index": 0
}

Now it's convenient to add new fields, just add new variables to the class and generate new json.
If you ask.

A
anyd3v, 2014-10-11
@anyd3v

Judging by the text of the error, you have a problem with the request. The text "insert ";" to complete the Statemen" is very similar to the errors that the database issues when the query is not correctly composed.

V
Vadim, 2014-10-11
@voznik

I read a couple of hours about escaping special characters in Java. changed the line to:
result:

'\{"metadata.title": "",
  "metadata.description": " 'Терапевт – лікар'",
  "metadata.keywords": "",
  "metadata.robots": "",
  "metadata.author": "",
  "config.enable_comments": 1,
  "config.primary_category": 205,
  "jbzoo.no_index": 0
\}'

but I need just a curly brace at the beginning of the line, not \{. if I escape it with only one slash - then an error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question