node-xmpp-server with json body messages - json

is there any chance to get node-xmpp-server lib to handle messages body in json format (XEP-0295)?

You can serialize json object using stringify, send it, and then unserialize it using parse method
Lets say we have jsonObject variable:
var data = {'some': 'thing', 'bla': 'blah'};
var message = JSON.stringify(data);
and unserialize:
var data = JSON.parse("some serialized message");

Related

RestSharp: Stackoverflow exception at Client.Execute

Hoping you'd be able to assist me with this error - I am getting
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
at the time when Client.Execute is called, I've shared my code below:
Thanks in Advance!
var Client = new RestClient();
Client.BaseUrl = new Uri("http://dummyurl.com/");
Client.Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator("username", "password");
var eventlist = new List<string>();
var request = new RestRequest(Method.POST);
request.AddHeader("event", "application/json");
string jsonBody = #"{'xxxxx':'xxxx',
'xxxx':'Xxxxx'}";
JObject jsonObject = JObject.Parse(jsonBody);
request.AddBody(jsonObject);
IRestResponse response = Client.Execute(request); //<<<--Throws Stackoverflow exception
var content = JsonConvert.DeserializeObject(response.Content);
It blows up on serialising the JObject instance. You don't need to overload your code with repeated serialization and JSON parsing.
To pass a ready-made JSON as the request body, just use AddParamerter(contentType, jsonString, ParameterType.RequestBody);
Set the content type accordingly to application/json or whatever your server wants.

extracting data from Json file

I am using a child process to execute a script and I have this result in stdout. Using res.json(stdout) to send this output to the variable data in app.component. How can I extract data from this output using data.TradeId for example knowing that the variable data is an object.
Console.log(data)
Query Result: {"TradeId":"FTE_2","BuyerTaxId":"ABC
Firm","Skuid":"SKU001","SellerTaxId":"CDE
Firm","ExportBankId":"","ImportBankId":"","DeliveryDate":"","ShipperId":"","Status":"Trade
initiated","TradePrice":10000,"ShippingPrice":1000}
So assuming "data" is a string (because you say console.log(data) equals the given output), you could do something like:
// This will be the data variable you already have
const data = `Query Result: {"TradeId":"FTE_2","BuyerTaxId":"ABC Firm","Skuid":"SKU001","SellerTaxId":"CDE Firm","ExportBankId":"","ImportBankId":"","DeliveryDate":"","ShipperId":"","Status":"Trade initiated","TradePrice":10000,"ShippingPrice":1000}`;
// Replace the prefixed string from the JSON string
const jsonString = data.replace("Query Result: ", "");
// Parse the json string into a json object
const jsonObject = JSON.parse(jsonString);
// At last, you can simply get the TradeId from your JSON object
const tradeId = jsonObject.TradeId;
console.log(tradeId); //results in: FTE_2

getting exception while geeting json data along with http files

i am sending the data through postman to asp.net webapi. But i am getting the exception.Need to send both file and json data.
Get the json format also in C# coding as string.
public JsonResult FileUploadDetails(HttpPostedFileBase[] Files,string request)
{
//Stream req = Request.InputStream;
//string reqparm = Request.QueryString["request"];
//req.Seek(0, System.IO.SeekOrigin.Begin);
//var jsonStr = new StreamReader(reqparm).ReadToEnd();
JObject obj = JObject.Parse(request);
.................
...............
}

How do I use the Apache HttpClient library to submit a PATCH request with JSON data?

I’m using Apache HTTP client v 4.3.4. How do I submit JSON data to a URL via the PATCH method? I have tried this
// Create the httpclient
HttpClient httpclient = HttpClientBuilder.create().build();
// Prepare a request object
HttpUriRequest req = null;
if (method.equals(RequestMethod.PATCH))
{
req = new HttpPatch(url);
req.setHeader("Content-type", "application/json");
if (jsonData != null)
{
final StringEntity stringData = new StringEntity(jsonData.toString());
req.setEntity(stringData);
} // if
but on the “req.setEntity” line, I get the compilation error, “The method is undefined”. Note that my request needs to send the JSON data as is, as opposed to putting it into a name-value param pair.
You have cast the HttpPatch object is implicity cast to HttpUriRequest in your code.
The HttpUriRequest interface does not support the setEntity method so you need to cast:
((HttpPatch)req).setEntity(stringData);

how to convert an object from json format to integer format

I am passing an array from view to the controller using json.stringify() method.
That value is passed to the controller and is in
"\"[{\\\"id\\\":1 "id\\\":2}]\"" format. I think this is in json format, I want to convert this into {id:1,id:2} format.
I tried to convert this into string format using JsonConvert.SerializeObject(),
but it is displaying in "\"\\\"[{\\\\\\\"id\\\\\\\":1}]\\\"\"" format.
Can you tell me how can I convert into {id:1,id:2} format/integer format?
The JSON data represents an array containing a dictionary as far as I can tell. You could get hold of a Dictionary representation of the data like this:
var json = "[{\"id\":1, \"id\": 2}]";
Dictionary<string, int>[] obj = JsonConvert.DeserializeObject<Dictionary<string, int>[]> (json);
var dict = obj[0];
I'm not sure if this is what you're after though, since you're saying that you want to display it like {id: 2}. If you want to print it out like that, you can build a string representation of the Dictionary:
var sb = new StringBuilder();
sb.Append("{");
foreach (var item in dict)
{
sb.Append(string.Format("{0}: {1}, ", item.Key, item.Value));
}
sb.Remove(sb.Length - 2, 2);
sb.Append("}");
Console.WriteLine(sb.ToString());
This prints {id: 2}.
To retrieve the value for 'id', if this is what you mean, you can do this:
var val = dict["id"];
var serializer = new JavaScriptSerializer();
var data = serializer.Deserialize<object[]>(jsonString);