Click here to Skip to main content
15,884,893 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!
I'm trying to send a JSON format through ActiveMQ (C# Console Application), but right now I'm stock in the following:
1) If I send the direct JSON serialization (as string) the system adds backward slashes "\" to the text, which makes my listener unable to process the information.

2) I can remove the backwar slashes by doing a JToken Parse, but I cannot send the var on ActiveMQ (ITextMessage requests a String, I'm sending a JToken) and of course if I do cast of the JToken to string I found again the backward slashes "\"

Any clue on how I can achieve my goal???

What I have tried:

Here is my current code:
String IP = "172.29.75.43:61616";
            String QueuesNameESF = "queue://MES2WMSTVOFFLINE";
            try
            {
                Uri _uri = new Uri(String.Concat("activemq:tcp://" + IP));
                IConnectionFactory factory = new ConnectionFactory(_uri);
                using (IConnection conn = factory.CreateConnection())
                {
                    using (ISession session = conn.CreateSession())
                    {
                        IDestination destination = SessionUtil.GetDestination(session, QueuesNameESF);
                        using (IMessageProducer producer = session.CreateProducer(destination))
                        {
                            SqlConnection dbConnection = new SqlConnection("Integrated Security=false;Data Source=smxsql08svr01;initial catalog=BSS;user id=sa;password=newshamu;Max Pool Size=75000");
                            SqlConnection internalConnection = new SqlConnection("Integrated Security=false;Data Source=smxsql08svr01;initial catalog=BSS;user id=sa;password=newshamu;Max Pool Size=75000");
                            String SearchPallet;
                            SqlCommand cmd = new SqlCommand();
                            dbConnection.Open();

                            SearchPallet = "";
                            SearchPallet = "Select Replace(Replace(Replace((Convert(varchar,GETDATE(),120) + Palletnum),'-',''),' ',''),':','')  as 'ID', ";
                            SearchPallet += "'Y' as 'beFull','ZJ' as 'ext1','' as 'ext2','Y'as 'isCM',Part_Number as 'itemCode',linea as 'line',";
                            SearchPallet += "Case When len(Part_Number) = 12 then RIGHT(Part_Number,7) else RIGHT(Part_Number,8) end as 'lot',";
                            SearchPallet += "Palletnum as 'mainPallet',GETDATE() as 'sendTime','BSS' as'sender','8170' as'werks' From Finishgoods ";
                            //Remove if not test
                            SearchPallet += "Where PalletNum = @Palletnum GROUP by PalletNum,Part_Number,linea";
                            cmd.CommandText = SearchPallet;
                            cmd.Connection = dbConnection;
                            cmd.Parameters.Add("@Palletnum", SqlDbType.VarChar).Value = "PT01929734";//Pallet;//"PT01929734";
                            SqlDataReader rdr = cmd.ExecuteReader();
                            FirstLevel product = new FirstLevel();
                            if (rdr.Read())
                            {
                                //FirstLevel product = new FirstLevel();
                                product.guid = rdr.GetValue(0).ToString();
                                product.beFull = rdr.GetValue(1).ToString();
                                SecondLevel msg = new SecondLevel();
                                msg.carton = rdr.GetValue(8).ToString();
                                List<ThirdLevel> serials = new List<ThirdLevel>();

                                ThirdLevel sn = new ThirdLevel();
                                String SearchSN = "Select barcodenum as 'serNo',''as 'ext1',''as 'ext2' From Finishgoods where palletnum = @palletnum";
                                SqlCommand cmd_sn = new SqlCommand();
                                internalConnection.Open();
                                cmd_sn.Connection = internalConnection;
                                cmd_sn.CommandText = SearchSN;
                                cmd_sn.Parameters.Add("@Palletnum", SqlDbType.VarChar).Value = "PT01929734";
                                SqlDataReader rdr_sn = cmd_sn.ExecuteReader();
                                while (rdr_sn.Read())
                                {
                                    sn.serNo = rdr_sn.GetValue(0).ToString();
                                    sn.ext1 = rdr_sn.GetValue(1).ToString();
                                    sn.ext2 = rdr_sn.GetValue(2).ToString();
                                    serials.Add(sn);
                                }
                                rdr_sn.Close();
                                cmd_sn.Parameters.Clear();
                                cmd_sn.Dispose();
                                internalConnection.Close();
                                msg.serNoMsg = serials;
                                product.crtonMsg = msg;
                                product.ext1 = rdr.GetValue(2).ToString();
                                product.ext2 = rdr.GetValue(3).ToString();
                                product.isCM = rdr.GetValue(4).ToString();
                                product.itemCode = rdr.GetValue(5).ToString();
                                product.line = rdr.GetValue(6).ToString();
                                product.lot = rdr.GetValue(7).ToString();
                                product.mainPallet = rdr.GetValue(8).ToString();
                                product.sendTime = rdr.GetValue(9).ToString();
                                product.sender = rdr.GetValue(10).ToString();
                                product.werks = rdr.GetValue(11).ToString();
                            }
                            rdr.Close();
                            string json_ = JsonConvert.SerializeObject(product);
                            var json = JToken.Parse(json_);
                            conn.Start();
                            ITextMessage request = session.CreateTextMessage(json); //Here I need to send the JSON
                            producer.Send(request);
                        }
                        Console.WriteLine("send succeed");
                        Console.ReadLine();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Receive Failed");
                Console.ReadLine();
            }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900