I use the following code to post :
try {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(VERIFY_PAYMENT_ACTIONURL);
// key is the parameter
// MERCHANT_KEY is the value
method.addParameter("form", "2");
method.addParameter("key", MERCHANT_KEY.trim());
method.addParameter("command", VERIFY_PAYMENT_COMMAND.trim());
method.addParameter("hash", hash);
method.addParameter("var1", transactionID.trim());
method.addParameter("salt", ALGORIHTM_SHA512_SALT_KEY.trim());
int statusCode = client.executeMethod(method);
if (statusCode != -1) {
in = method.getResponseBodyAsStream();
}
String text = method.getResponseBodyAsString();
System.out.println("text : "+text);
method.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
and I get the response text as follows :
a:3:{s:6:"status";i:1;s:3:"msg";s:44:"1 out of 1 Transactions Fetched Successfully";s:19:"transaction_details";a:1:{i:2298597;a:15:{s:8:"mihpayid";s:18:"403993715508098532";s:10:"request_id";N;s:12:"bank_ref_num";N;s:3:"amt";s:5:"53.77";s:4:"disc";s:4:"0.00";s:4:"mode";s:2:"CC";s:7:"PG_TYPE";s:4:"AXIS";s:7:"card_no";s:16:"512345XXXXXX2346";s:12:"name_on_card";s:3:"emu";s:4:"udf2";s:1:"0";s:7:"addedon";s:19:"2013-06-03 17:34:42";s:6:"status";s:7:"failure";s:14:"unmappedstatus";s:6:"failed";s:12:"Merchant_UTR";N;s:10:"Settled_At";N;}}}
now I want to extract the above output and put it into map like "transactionid" and other details like the following format
array('status' => '1',
'msg' => 'Transaction Fetched Successfully',
'transaction_details' =>
array(
'mihpayid' => Transaction ID,
'request_id' => Request ID,
'bank_ref_num' => Bank Reference Number,
'amt' => Amount
'disc' => Discount
'mode' => Transaction Mode (NB for Netbanking, CC for credit card, DC for Debit card, "-" for
'status' => Transaction Status
unknown)
)
);
I really do not find any common way to extract the above output. will some one help to do this?
or is it possible for me to convert the above to JSON and put them in to a map like the above?
thanks
I don't recognise it but are you sure it's not a well know format?
To turn it into JSON you'd need to
1)remove every instance of a character that is followed by a colon (and the colon)
2)replace every other semi-colons with a comma
3)replace all the other semi-colons with colons
And you'd need to do all that while taking into account that strings could contain any of those things.
From comments:
The string is in PHP serialised form. This library parses it: https://code.google.com/p/serialized-php-parser/
Related
I am testing RedisGraph as a way to store my data which originates from a client as JSON.
The JSON passes through a bean for validation etc and I use Jackson to serialise the bean so the RedisGraph string is in the correct format. For completeness on that formatting step see the sample code at the end.
The data properties might contain sinqle quotes in valid JSON format eg: O'Toole
{ "name" : "Peter O'Toole", "desc" : "An actors actor" }
I can use a formatter as per the code block at the end to get the JSON into a format the RedisGraph command will allow which copes with the single quotes (without me needing to escape the data content - ie it can use what the client sends). eg this works:
GRAPH.QUERY movies "CREATE (:Actor {name:\"Peter O'Toole\", desc:\"An actors actor\", actor_id:1})"
So far, so good.
Now, the problem: I am having trouble with the syntax to persist original JSON where it ALSO contains escaped double quotes. eg:
{ "name" : "Peter O'Toole", "desc" : "An \"actors\" actor" }
I don't want to have to escape or wrap the desc property value because it is already escaped as valid JSON. But then how do I construct the RedisGraph command so it persists the properties using the values it is given? ie containing escaped double quotes.
In other words, this throws a parsing error because of the \" in the desc property.
GRAPH.QUERY movies "CREATE (:Actor {name:\"Peter O'Toole\", desc:\"An \"actors\" actor\", actor_id:1})"
Given it would be quite common to want to persist data containing valid JSON escaped double quotes \" AND unescaped single quotes, there must be a way to do this. eg name and address data.
Any ideas?
Thanks, Murray.
PS: this doesnt work either: it chokes on the embedded ' in O'Toole
GRAPH.QUERY movies "CREATE (:Actor {name:\'Peter O'Toole\', desc:\'an \"actors\" actor\', actor_id:3})"
// \u007F is the "delete" character.
// This is the highest char value Jackson allows and is
// unlikely to be in the JSON (hopefully!)
JsonFactory builder = new JsonFactoryBuilder().quoteChar('\u007F').build();
ObjectMapper objectMapper = new ObjectMapper(builder);
// Set pretty printing of json
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
// Do not surround property names with quotes. ie { firstName : "Peter" }
objectMapper.configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(), false);
// Make a Person
Person person = new Person("Peter", "O'Toole");
// Set the desc property using embedded quotes
person.setDesc("An \"actors\" actor");
// Convert Person to JSON
String json = objectMapper.writeValueAsString(person);
// Now convert your json to escape the double quotes around the string properties:
String j2 = json.replaceAll("\u007F", "\\\\\"");
System.out.println(j2);
This yields:
{
firstName : \"Peter\",
lastName : \"O'Toole\",
desc : \"An \"actors\" actor\"
}
which is in a format Redis GRAPH.QUERY movies "CREATE..." can use (apart from the issue with \"actors\" as discussed above).
OK. The issue was an artefact of trying to test the syntax by entering the commands into RedisInsight directly. As it turns out all one needs to do is to remove the double quotes from the valid json.
So, to be clear, based on normal valid json coming from the client app,
the formatter test is:
ObjectMapper objectMapper = new ObjectMapper();
// (Optional) Set pretty printing of json
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
// Do not surround property names with quotes. ie { firstname : "Peter" }
objectMapper.configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(), false);
// Make a Person
// For this example this is done directly,
// although in the Java this is done using
// objectMapper.readValue(incomingJson, Person.class)
Person person = new Person("Peter", "O'Toole");
// Set the desc property using escaped double quotes
person.setDesc("An \"actor's\" actor");
// Convert Person to JSON without quoted property names
String json = objectMapper.writeValueAsString(person);
System.out.println(json);
yields:
{
firstname : "Peter",
lastname : "O'Toole",
desc : "An \"actor's\" actor"
}
and the command string is consumed by the Vertx Redis:
Vertx vertx = Vertx.vertx();
private final Redis redisClient;
// ...
redisClient = Redis.createClient(vertx);
String cmdStr = "CREATE (:Actor {firstname:"Peter", lastname: "O'Toole", desc:"An \"actor's\" actor", actor_id:1})";
Future<String> futureResponse = redisClient.send(Request.cmd(Command.GRAPH_QUERY).arg("movies").arg(cmdStr))
.compose(response -> {
Log.info("createRequest response=" + response.toString());
return Future.succeededFuture("OK");
})
.onFailure(failure -> {
Log.error("createRequest failure=" + failure.toString());
});
:-)
While processing the DialogFlow Response object, I get the below given string as textPayload. If this is a Json string, I can easily convert it to a JSONObject and then extract the values. However, could not convert this to a Json Object. How do I get the values for the keys in this string? What is a good way to parse this string in Java?
String to be processed
Dialogflow Response : id: "XXXXXXXXXXXX"
lang: "en"
session_id: "XXXXX"
timestamp: "2020-04-26T16:38:26.162Z"
result {
source: "agent"
resolved_query: "Yes"
score: 1.0
parameters {
}
contexts {
name: "enaccaccountblocked-followup"
lifespan: 1
parameters {
}
}
metadata {
intent_id: "XXXXXXXXXXXX"
intent_name: "EN : ACC : Freezing Process - Yes"
end_conversation: true
webhook_used: "false"
webhook_for_slot_filling_used: "false"
is_fallback_intent: "false"
}
fulfillment {
speech: "Since you have been permanently blocked, please request to unblock your account"
messages {
lang: "en"
type {
number_value: 0.0
}
speech {
string_value: "Since you have been permanently blocked, please request to unblock your account."
}
}
}
}
status {
code: 200
error_type: "success"
}
Convert it to valid json, then map using one of the many libraries out there.
You'll only need to:
replace "Dialogflow Response :" with {
add } to the end
add commas between attributes, ie
at the end of every line with a ":"
after "}", except when the next non-whitespace is also "}"
Jackson (at least) can be configured to allow quotes around attribute names as optional.
Deserializing to a Map<String, Object> works for all valid json (except an array, which this isn't).
If I understand you correctly the issue here is that the keys do not have quotations marks, hence, a JSON parser will reject this.
Since the keys all start on a new line with some white-space and all end with a colon : you can fix this easily with a regular expression.
See How to Fix JSON Key Values without double-quotes?
You can then parse it to a Map via
Map<String, Object> map
= objectMapper.readValue(json, new TypeReference<Map<String,Object>>(){});
(but I assume you are aware of this).
Create a class for TextPayload object like this.
public class TextPayload {
private int session_id;
private String lang;
private String timestamp;
private String[] metadata ;
//Other attributes
//getters setters
}
Then using an ObjectMapper extract the values from textpayload like this:
ObjectMapper mapper = new ObjectMapper();
TextPayload textPayload = mapper.readValue(output, User.class);
To utilize ObjectMapper and hands on with it follow this
you can use the nodejs package parse-dialogflow-log to parse the textResponse string.
replace "Dialogflow Response :" with "{"
add "}" to the end
run the package on the result and you'll get a nice json.
Maybe its a very newbie question for here but i spend a lot of time to see a good approach for doing this and i didn't find convenient answer. I am trying to make a simple call to a rest api and i want to pass a value with the GET request appended to the string. like url/foo where foo is the parameter. I have a query variable that i want to append to the end of the url string for the get request. Thank you in advance.
class About extends React.Component {
constructor(props) {
super(props);
this.state = {
products: [],
filteredItems: [],
user: {},
query: '' <-- query variable to be appended to the end of the get request
};
}
componentDidMount() {
fetch(`'myurl/${this.state.query}'`) <-- i want to append the variable at the end of the string ??
.then(res => res.json())
.then((result) => {
console.log(result);
this.setState({
products: result,
filteredItems: result
});
}
)
}
queryChange = (evt) => {
this.setState({query: evt.target.value}) <-- update the variable state from an event
}
Get rid of the extra quotations (') in 'myurl/${this.state.query}'
let query = {id:1};
let url = 'https:example.com//xyz.com/search?' + query;
fetch(url)
You can pass param without using `` or $ also,In componentDidMount()
componentDidMount() {
let query = this.state.query;
fetch('myurl/'+query)
.then(res => res.json())
.then((result) => {
console.log(result);
this.setState({
products: result,
filteredItems: result
});
}
)
}
I am trying to print some data which is in an array format. But while printing I am loosing my initial byte of the array which I need. How can I get that?
rray
(
[0] => stdClass Object
(
[company_category] => Company limited by Shares
[email_id] => example#domain.com
[last_processed] => 2016-07-01T08:42:34Z
[company_class] => Public
[country] =>
)
)
As you will be seeing that starting of the output should have be "Array" but it is not. What am I doing wrong here. As I am sending a request to a specific URL and getting a response from it. Below is the code I use to get the reponse
HttpEntity entity = CloseableHttpResponse.getEntity();
String response = EntityUtils.toString(entity,"UTF-8");
System.out.println(response);
I managed to get a json response back from a request and i convert it to String lets say:
String response = client.execute(get, handler);
The response looks something like:
"geometry":{"rings":[[[29470.26099999994,40220.076999999583],[29551.560999999754,40324.093000000343],[29597.470999999903,40391.253000000492],[29619.849999999627,40434.842000000179],[29641.708999999799,40471.713999999687],[29701.501000000164,40574.616000000387],[29722.775999999605,40611.230000000447],[29723.673000000417,40613.234999999404]]]}
But I want to have it to look like the following one:
"Coordinates":"29470.26099999994|40220.076999999583,29551.560999999754|40324.093000000343,29597.470999999903|40391.253000000492,45360.235000003|41478.4790000003,45360.2369999997|41478.4729999993,45353.8320000004|41470.7339999992,45372.21|41468.057,45371.8090000004|41467.1390000004"
In summary i want to change the comma between two coordinates in a [ ] set to be separated by pipes"|" instead of comma and to separate a set of two coordinates with , instead of "],["
What i tried:
response = response.replace("],[","\,");
response = response.replace("[[[","\"");
response = response.replace("]]]","\"");
However it does not give me what i wanted...becuz i have no idea to achieve the replace of pipe...tot of using regex but dont know how to. can someone help me please
try something like this
String result = response.replaceAll("([^\\]])(\\,)","$1\\|").replaceAll("[\\[\\]]","");
=> ([^\\]])(\\,) => ([^\])(\,) every comma not preceded by ]
=> [\\[\\]] => [\[\]] every [ or ]
Please note that
replacing using regexp is using String.replaceAll or Pattern class
String are immutable
I think this should work:
String response = "[[[29470.26099999994,40220.076999999583],[29551.560999999754,40324.093000000343],[29597.470999999903,40391.253000000492],[29619.849999999627,40434.842000000179],[29641.708999999799,40471.713999999687],[29701.501000000164,40574.616000000387],[29722.775999999605,40611.230000000447],[29723.673000000417,40613.234999999404]]]";
response = response.replace(",", "|");
response = response.replace("]|[", ",");
response = response.replace("[[[", "\"");
response = response.replace("]]]", "\"");
System.out.println(response);