To decode API response string to JSON, json.decode() works fine.
This will parse a JSON string similar to
{ "Response" : {"Responsecode" : "1" , "Response" : "Success"}}
But in my case, the response comes in the serialized form like:
{\"Response\" : {\"Responsecode\" : \"0\" , \"Response\" : \"Success\"}}
json.decode() won’t work.
In Java, I used StringEscapeUtils.unescapeJson() for the same problem.
I searched for Dart but couldn’t find how to unescape characters in a string.
Edit:
Suppose, the value of key data is abc"de
So, its corresponding JSON would be {"data":"abc\"de"}
And hence during serialization, this json string is escaped to give {\"data\":\"abc\\\"de\"} as the response, which is sent by the API.
So, my intention is to remove the escape sequences, so that I can get the string {"data":"abc\"de"}, which would later be decoded using json.decode(). Removing the escape sequences was done using StringEscapeUtils.unescapeJson() in java.
json.decode can decode single strings too, so you should be able to just call it twice. The first time it'll return you a string (where the escape characters have been decoded) and the second time it'll decode that string into the map:
import 'dart:convert';
void main() {
var a = r'''"{\"Response\" : {\"Responsecode\" : \"0\" , \"Response\" : \"Success\"}}"''';
var b = json.decode(json.decode(a));
print(b['Response']['Responsecode']); // 0
print(b['Response']['Response']); // Success
}
Related
I have a json stored as string like below
String json="[{"name":"a","id",1},{"name":"b","id",2},{"name":"c","id",3}]";
My Question how to encode this to a map or a list to get access to the keys and use the values?
You need to JSON-decode the value first
import 'dart:convert';
final decoded = jsonDecode(json);
print(decoded[0]['name']); // just one example
I'm new to Go. My question is how to get URL encoded string on stdout.
Below is the URL string I am using to hit an api.
schooltubeapi/v1/channeldetails?channelName=long%20division%20.
Below is the code that I am using to get RawQuery
url1 := ChannelName
u, _ := url.Parse(url1)
log.Println(u)
u.RawQuery = u.Query().Encode()
log.Println(u)
[Output]
long division
[Expected]
long%20division%20
I have searched alot But cannot found a similar problem with a solution.
For url encoded string use URL struct of url package to get RawQuery as passed in the URI:
package main
import (
"fmt"
"net/url"
)
func main() {
stringValue := "long division "
t := &url.URL{Path: stringValue}
encodedString := t.String()
fmt.Println(encodedString)
}
Playground Example
In Golang spec for URL. It is stated:-
that the Path field is stored in decoded form: /%47%6f%2f becomes
/Go/. A consequence is that it is impossible to tell which slashes in
the Path were slashes in the raw URL and which were %2f. This
distinction is rarely important, but when it is, code must not use
Path directly. The Parse function sets both Path and RawPath in the
URL it returns, and URL's String method uses RawPath if it is a valid
encoding of Path, by calling the EscapedPath method.
type URL struct {
Scheme string
Opaque string // encoded opaque data
User *Userinfo // username and password information
Host string // host or host:port
Path string // path (relative paths may omit leading slash)
RawPath string // encoded path hint (see EscapedPath method)
ForceQuery bool // append a query ('?') even if RawQuery is empty
RawQuery string // encoded query values, without '?'
Fragment string // fragment for references, without '#'
}
For more information Check Golang spec for URL
I am trying to extract the case-insensitive query parameter /staging/ec/23463/front-view-72768.jpg?angle=90&or=0x0&wd=400&ht=200 from the URL. When I try to convert the whole URL in lowercase it throws the following exception :
cannot use r.URL (type *url.URL) as type string in argument to strings.ToLower
I printed the value of URL which says underlying it stores all the query strings as map i.e. map[angle:[90] or:[0x0] wd:[400] ht:[200]]. Hence I will get the correct value using this r.URL.Query().Get("or") But if query string comes out Or. It will fail.
*URL.Query() returns a value of type url.Values, which is just a map[string][]string with a few extra methods.
Since URL values are by definition case-sensitive, you will have to access the map directly.
var query url.Values
for k, vs := range query {
if strings.ToLower(k) == "ok" {
// do something with vs
}
}
Try it on the playground: https://play.golang.org/p/7YVuxI3GO6X
cannot use r.URL (type *url.URL) as type string in argument to strings.ToLower
This is because you are passing ur.URL instead of string. Get the string from url through String() function.
url.String()
public class MsgPackInRedis {
private String ip;
private int port;
private String session;
private String protocol;
}
MsgPackInRedis msgPackStringInRedis = new MsgPackInRedis();
I encode a java object msgPackStringInRedis of class MsgPackInRedis with msgpack, then store in redis.
And I want to decode that in lua, which runs in redis, how can I get "session" ?
Can I do like this below, get session by index 3?
local msgPackObject = cmsgpack.unpack(msgPackStringInRedis)
local session = msgPackObject[3]
MessagePack is an encoding - think non-easily-readable JSON. In fact, this website does a back and forth translation between the two: http://kawanet.github.io/msgpack-lite/
Feeding your (0x94 0xc0 0x00 0xa4 0x41 0x42 0x43 0x44 0xc0) to the above website, you can see the JSON representation which looks like:
[
null,
0,
"ABCD",
null
]
You can test that in Redis' Lua as well, e.g. (note that Lua 5.1 accepts decimal byte representation, hence the different representation of the same payload in the example):
$ redis-cli EVAL "return(cmsgpack.unpack('\148\192\00\164\65\66\67\68\192')[3])" 0
"ABCD"
So frankly, I see no issue with your code. What is the problem that you are experiencing exactly?
Assuming that your MessagePack-ed data is stored in the String key called foo, this would do your bidding:
EVAL "return cmsgpack.unpack(redis.call('GET', KEYS[1]))" 1 foo
Note : the above assumes that the data is serialized as arrays. Returning an object will not work as Redis' protocol doesn't support that.
I am supplied the following RSA private key in the format
<RSAKeyValue>
<Modulus>XXXXXXXX</Modulus>
<Exponent>XXXXXXXX</Exponent>
<P>XXXXXXXX</P>
<Q>XXXXXXXX</Q>
<DP>XXXXXXXX</DP>
<DQ>XXXXXXXX</DQ>
<InverseQ>XXXXXXXXXX/InverseQ>
<D>XXXXXXXX</D>
</RSAKeyValue>
The XXXX are in Base64 format.
I want to know how to combine it all the XXXXXX bits to a single Base64 string.
With this single Base64 string i do the following:
1. Feed it to a TMemorStream
2. use Indy's TIdDecoderMIME class to decode Base64 from the MemoryStream
3. The decoded MemoryStream is then feed into CryptDecrypt function from wcrypt2.pas (a delphi wrapper of Microsoft's Cryptographic API) from Jedi
I know the solution for public key in the same format
<RSAKeyValue>
<Modulus>xqiYKv0umaLdmrKPyBfYmAfzZYVsvsOJyS4c1lBPjqpn7zh+XyxPXK7MxJkAlenQJM33M+ZYfmlPLya7JWXXTPviylEEtlmul9GshpX2caxWu2YO9vNIHRZYYau4ccbkm95iMyJi8KN2ANtqDwiJv55vcXZDqjPSDE4ap49xmog==</Modulus>
<Exponent>AAQC</Exponent>
</RSAKeyValue>
The solution is to add "BgIAAACkAABSU0ExAAQAAAE" + Exponent + Modulus
The result is:
BgIAAACkAABSU0ExAAQAAAEAAQCxqiYKv0umaLdmrKPyBfYmAfzZYVsvsOJyS4c1lBPjqpn7zh+XyxPXK7MxJkAlenQJM33M+ZYfmlPLya7JWXXTPviylEEtlmul9GshpX2caxWu2YO9vNIHRZYYau4ccbkm95iMyJi8KN2ANtqDwiJv55vcXZDqjPSDE4ap49xmog==
With the private key how do we combine it? I know it starts off like this:
"BwIAAACkAABSU0ExAAQAAAE" + Exponent + Modulus + ???????
The XXXX in the RSAKeyValue XML are in base64, just that i do not want to expose the details there. I want to know how do i combine all the XXXX base64 codes into a single base64 private key.
I suspect that this means that you are performing the base64 encoding line by line. It's much simpler to perform the encoding on the entire file.
For example you might do this as follows:
Load the file into a TStringList.
Extract a single string representing the file using the Text property of the string list.
Base64 encode that string.
Send it over the wire.
At the receiving end, decode the string.
Assign the string to the Text property of a string list.