Удалить возврат каретки из текста json

Я получаю текст json, возвращаемый вызовом API, и запускаю его с помощью скрипта Сериализация и десериализация JSON отсюда https://www.mql5.com/en/code/13663.

Моя проблема в том, что он обрабатывает только первые несколько частей json из-за того, что я считаю разрывом строки/кареткой внутри структуры json. Я не получаю никакого сообщения об ошибке, просто массив, содержащий все до разрыва строки.

Я не хочу удалять разрывы строк внутри текстовых полей в json, только возвращаемые значения внутри структуры json. Он на месте каждый раз сразу после {"ok":true,"result":[{"update_id":568022212,

Вот полный раздел

{"ok":true,"result":[{"update_id":568022212,
"channel_post":{"message_id":436,"chat":{"id":-1001436032340,"title":"FORTUNA","type":"channel"},"date":1588899840,"reply_to_message":{"message_id":372,"chat":{"id":-1001436032340,"title":"FORTUNA","type":"channel"},"date":1588838583,"text":"A\nGbpusd buy now 1.2360\nSl 1.2280 \nTp open\n","entities":[{"offset":52,"length":11,"type":"mention"}]},"text":"A\n42 pips bookd close\ud83d\udfe2\ud83d\udfe2"}},{"update_id":568022213,
"channel_post":{"message_id":437,"chat":{"id":-1001436032340,"title":"FORTUNA","type":"channel"},"date":1588900321,"reply_to_message":{"message_id":435,"chat":{"id":-1001436032340,"title":"FORTUNA","type":"channel"},"date":1588893671,"text":"A\nGold buy 1713.3\nSl 1702 \nTp open\nSwing trade"},"text":"Amazon\n60 pips bookd close\ud83d\udfe2\ud83d\udfe2"}},{"update_id":568022214,
"channel_post":{"message_id":438,"chat":{"id":-1001436032340,"title":"FORTUNA","type":"channel"},

MQL4-код:

 int   GetUpdates()
     {
      if(m_token==NULL)
         return(ERR_TOKEN_ISEMPTY);

      string out;
      string url=StringFormat("%s/bot%s/getUpdates",TELEGRAM_BASE_URL,m_token);
      string params=StringFormat("offset=%d",m_update_id);
      //---
      int res=PostRequest(out,url,params,WEB_TIMEOUT);
      if(res==0)
        {

         Print(StringDecode(out));
         //--- parse result
         CJAVal js(NULL,jtUNDEF);
         bool done=js.Deserialize(out);
         if(!done)
            return(ERR_JSON_PARSING);

         bool ok=js["ok"].ToBool();
         if(!ok)
            return(ERR_JSON_NOT_OK);

         CCustomMessage msg;

         int total=ArraySize(js["result"].m_e);
         for(int i=0; i<total; i++)
           {
            CJAVal item=js["result"].m_e[i];
            //---
            msg.update_id=item["update_id"].ToInt();

            //---
            msg.message_id=item["message"]["message_id"].ToInt();
            msg.message_date=(datetime)item["message"]["date"].ToInt();
            //---
            msg.message_text=item["message"]["text"].ToStr();
            printf((datetime)item["message"]["date"].ToInt());
            msg.message_text=StringDecode(msg.message_text);
            //---
            msg.from_id=item["message"]["from"]["id"].ToInt();

            msg.from_first_name=item["message"]["from"]["first_name"].ToStr();
            msg.from_first_name=StringDecode(msg.from_first_name);

            msg.from_last_name=item["message"]["from"]["last_name"].ToStr();
            msg.from_last_name=StringDecode(msg.from_last_name);

            msg.from_username=item["message"]["from"]["username"].ToStr();
            msg.from_username=StringDecode(msg.from_username);
            //---
            msg.chat_id=item["message"]["chat"]["id"].ToInt();

            msg.chat_first_name=item["message"]["chat"]["first_name"].ToStr();
            msg.chat_first_name=StringDecode(msg.chat_first_name);

            msg.chat_last_name=item["message"]["chat"]["last_name"].ToStr();
            msg.chat_last_name=StringDecode(msg.chat_last_name);

            msg.chat_username=item["message"]["chat"]["username"].ToStr();
            msg.chat_username=StringDecode(msg.chat_username);

person Keith Power    schedule 08.05.2020    source источник


Ответы (1)


Пробовал использовать эту же библиотеку, у нее похоже баг с парсингом массивов. Вот почему я использовал https://www.mql5.com/ru/code/11134. У него есть недостаток: вам нужно удалить все объекты, к сожалению; в результате есть много кода. Но, по крайней мере, это работает.

Кажется, ваш json имеет неправильный формат, я немного его обрезал.

  #include <json1.mqh> //modified version, https://www.mql5.com/en/forum/28928/page5#comment_15766620

  //const string post_result=
  //{"ok":true,"result":[
  //{"update_id":568022212,"channel_post":{"message_id":436,"chat":{"id":-1001436032340,"title":"FORTUNA","type":"channel"},
  //"date":1588899840,"reply_to_message":{"message_id":372,"chat":{"id":-1001436032340,"title":"FORTUNA","type":"channel"},
  //"date":1588838583,"text":"A\nGbpusd buy now 1.2360\nSl 1.2280 \nTp open\n","entities":[{"offset":52,"length":11,"type":"mention"}]},"text":"A\n42 pips bookd close\ud83d\udfe2\ud83d\udfe2"}},
  //{"update_id":568022213,"channel_post":{"message_id":437,"chat":{"id":-1001436032340,"title":"FORTUNA","type":"channel"},
  //"date":1588900321,"reply_to_message":{"message_id":435,"chat":{"id":-1001436032340,"title":"FORTUNA","type":"channel"},
  //"date":1588893671,"text":"A\nGold buy 1713.3\nSl 1702 \nTp open\nSwing trade"},"text":"Amazon\n60 pips bookd close\ud83d\udfe2\ud83d\udfe2" }}]}
  //;


void function()
  {
//---
  const string post_result=getContent();//ok, you managed to get some string here

  JSONParser *parser=new JSONParser();
  JSONValue *value=parser.parse(post_result);
  delete(parser);
  if(CheckPointer(value)==POINTER_INVALID)
    {
     printf("%i %s: error!",__LINE__,__FILE__);
     delete(value);
     return;
    }
  JSONObject *obj=(JSONObject*)value;
  const bool isSuccess=obj.getBool("ok");
  printf("%i %s: isSuccess=%d",__LINE__,__FILE__,isSuccess);
  if(!isSuccess)return;

  JSONValue *resultValue=obj.getValue("result");
  if(CheckPointer(resultValue)!=POINTER_INVALID)
    {
     JSONArray *resultValueArray=resultValue;
     for(int i=0;i<resultValueArray.size();i++)
       {
        printf("%i %s: #%d=%s",__LINE__,__FILE__,i,resultValueArray.getObject(i).toString());
        //you can work with JSONObject or with string, whatever is more convenient
        parseResultLine(resultValueArray.getObject(i));
       }
     delete(resultValueArray);
    }
  delete(resultValue);
  delete(obj);
}   
  bool parseResultLine(JSONObject *object)
    {
     const long update_id=object.getLong("update_id");
     JSONObject *channel_post=object.getObject("channel_post");

     const long message_id=channel_post.getLong("message_id");
     const datetime date=(datetime)channel_post.getInt("date");
     const string text=channel_post.getString("text");
  printf("%i %s: id=%s, dt=%d/%s, text=%s",__LINE__,__FILE__,IntegerToString(message_id),(int)date,TimeToString(date),text);
     JSONObject *chat=channel_post.getObject("chat");
     const long chat_id=chat.getLong("id");
     const string chat_title=chat.getString("title");
  printf("%i %s: chat-> id=%I64d title=%s type=%s",__LINE__,__FILE__,chat_id,chat_title,chat.getString("type"));
     JSONObject *reply=channel_post.getObject("reply_to_message");
  printf("%i %s: replied: id=%s, date=%s",__LINE__,__FILE__,string(reply.getLong("message_id")),TimeToString(reply.getInt("date")));

     return true;
    }
person Daniel Kniaz    schedule 09.05.2020
comment
Спасибо за ответ. К сожалению, я получаю сообщение об ошибке в самой последней строке '#endif' - unexpected token. Вы сами с этим сталкивались? - person Keith Power; 09.05.2020
comment
Привет, Даниэль, я только что прокомментировал endif, и все работает, спасибо. Последний вопрос, если можно. Я попытался найти в Google, как получить поля из объекта, как в моем вопросе, но я запутался. У вас есть короткий пример. Спасибо - person Keith Power; 10.05.2020
comment
@KeithPower Хорошо, я обновил свой пример, посмотрите, пожалуйста. Не все ваши поля там, но я надеюсь, что этого будет достаточно, чтобы понять концепцию. printf поможет разобраться в остальном :) - person Daniel Kniaz; 10.05.2020
comment
удивительно спасибо. Я гораздо лучше разбираюсь в objects ???? - person Keith Power; 10.05.2020