How would I split this string? - java

How would I go about cutting this giant string? I need the data of the first ID," preferably for the string output to be:
"id":1,"name":"site1","has_doneit":true,"destination":"4613"
Is this possible? Or if not any other ways of grabbing the name:"site1" and the has_doneit:true would be perfectly fine.
{"needs_complete":true,"has_done":true,"sites":[
{"id":1,"name":"site1","has_doneit":true,"destination":"4613"},{"id":2,"name":"site2","has_doneit":true,"destination":"4613"},{"id":3,"name":"site3","has_doneit":true,"destination":"4339"},{"id":4,"name":"site4","has_doneit":true,"destination":"4340"},{"id":5,"name":"site5","has_doneit":true,"destination":"4341"},
{"id":6,"name":"site6","has_doneit":true,"destination":"4622"},{"id":7,"name":"site7","has_doneit":true,"destination":"4623"},{"id":8,"name":"site8","has_doneit":true,"destination":"4828"},
{"id":9,"name":"site9","has_doneit":true,"destination":"4829"},{"id":10,"name":"site10","has_doneit":true,"destination":"4861"}]}

That seems like a JSON string so use a Json Parser...
PHP: JSON_decode
$parsedStr = json_decode($yourString, true);
you can access sites array in $parsedStr['sites']
So, to access the id of the first site:
echo $parsedStr['sites'][0]['id'];
Java
check this answer in SO Decoding JSON String in Java

Related

How to url encode data in Java

So Im trying to translate a working python code into Java. One of the steps required is to url encode the data. But when I encode the data in Java it looks different than the one in encoded in Python.
In one of the block of Python code theres this:
data = {'request-json': json}
print('Sending form data:', data)
data = urlencode(data)
data = data.encode('utf-8')
print('Sending data:', data)
The Output
Sending form data: {'request-json': '{"apikey": "xewpjipcpovwiiql"}'}
The output after being encoded
Sending data: b'request-json=%7B%22apikey%22%3A+%22xewpjipcpovwiiql%22%7D'
So this is what im trying to do in Java. As you can imagine Java is more involved. I used gson to convert to Json
Gson gson = new Gson();
API_Key key = new API_Key("xewpjipcpovwiiql");
String jsonInputString = gson.toJson(key);
Data data = new Data(key);
String request_form = gson.toJson(data);
System.out.println(request_form);
String urlencoded = URLEncoder.encode(request_form,StandardCharsets.UTF_8);
System.out.println(urlencoded);
The output:
Sending form data: {"request-json":{"apikey":"xewpjipcpovwiiql"}}
The output of the encoded string:
%7B%22requestjson%22%3A%7B%22apikey%22%3A%22xewpjipcpovwiiql%22%7D%7D
So they dont look the same so why are they coming differently ? How do I get the same python encoded String in Java ? I noticed in Python it used a combination of single and double quotes and in Java its only Double quotes so I dont know if that makes a difference.
Thank You!
On the Python side: The data.encode('utf-8') call is not necessary or at least the documentation describes with a different intention compared to this use https://docs.python.org/3/library/stdtypes.html#str.encode (and that's why there's a b' at the beggining).
The outer brackets are missing because it is interpreting request-json as the URL parameter name (it may be easier to understand if you add a second property at the json's top/first property level, you'll see you end with request-json=%7B%22apikey%22%3A+%22xewpjipcpovwiiql%22%7D&second-property=<second-property-value>).
On the Java side: the request_form is being completely interpreted as a single value to encode so you can put the encoded value as part of some parameter in a URL, as in: https://host:port?some-parameter-name=%7B%22requestjson%22%3A%7B%22apikey%22%3A%22xewpjipcpovwiiql%22%7D%7D

How to convert JSON to a string with escape characters

I am new to JMeter java and Minidev. I am working on this existing module where I have to use minidev libraries I need to convert a JSON form non-indented format to a string format in Jmeter. The API requires a String representation of the JSON due to some characters.
To give an example, I have {"Item":"Value","MyList":["A","B","C"]} which I want to convert as "{\"Item\":\"Value\",\"MyList\":[\"A\",\"B\",\"C\"]}"
I have tried using JSONObject.toString() or JSONObject.toJSONString() however this does not give me desired results. I am using Jmeter 5 with Java 12
I did not find any good solution in short time so right now I am using below code however I would love to do it the right way.
String postJSON = root.toJSONString().replace("\"","\\\"");
postJSON = postJSON.replace("{","\"{");
postJSON = postJSON.replace("}","}\"");

How to put the string in the JSON and get the value in Java

I want to get the value of String and I am trying to put the string in the JSON and get it. The format of string is "key=value&key1=value". The code I am using is:
JSONObject json = new JSONObject(String);
String value = json.get("key").toString();
That string is url encoded not json formatted.
You can use a url decoder instead. Something like http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html might work for you.
I used Patterns and Matchers and it worked perfectly. Thank you.

Modify JSON database values with java in eclipse

I want to use eclipse and java to make a program that can parse json files in java only.
For example, I want to change a value in JSON to another value every second.
I know the jsonparse function in javascript, but to carry it over through scriptengine alone seems like a bad solution.
I also know that I can use Apache IO to read a file to string and then edit the string and read the file back, but it doesn't seem efficient.
Is there an effective way to modify values in a JSON.php file?
Here is a JSON.php file that I have.
[{"color": "red waves-effect waves-light", "value": "0", "activator": "null"}]
Here is a part of my java program that does the text editing.
String attentionVs = "" + attentionV;
File fileattentionV = new File("attentionV.php");
String fileContext2 = FileUtils.readFileToString(fileattentionV);
fileContext2 = attentionVs;
FileUtils.write(fileattentionV, fileContext2);
String meditationVs = "" + meditationV;
File filemeditationV = new File("meditationV.php");
fileContext2 = FileUtils.readFileToString(filemeditationV);
fileContext2 = meditationVs;
FileUtils.write(filemeditationV, fileContext2);
Thanks for your help!
Going on just the first sentence where you say you want to parse a JSON file in Java, have you seen this post How to parse JSON in Java

Android parse special characters in json response

In my Android application I get JSON response string from a PHP url. from the response I get some hotel names with apostrophe, I get &#039 character instead of apostrophe. How can I parse the hotel with special characters in android? I can see the apostrophe in the browser but could not see in android logcat.
I have tried jresponse = URLEncoder.encode(jresponse,"UTF-8"); but I could not get apostrophe for hotel name.
This is the one of the hotel name in the response.
I see the following in browser.
{"id":12747,
"name":"Oscar's",
....
}
But in the logcat:
id 12747
name Oscar's
Use the decoder instead of encoder. URLDecoder.decode(jresponse,"UTF-8")
Use ISO-8859-2 when you create the URLEncodedEntity that you send off. You can set this as a parameter in the constructor.
Without a specified charset, you are probably sending the data in UTF-8/UTF-16 (most common) which the server is interpreting in a different way.
EDIT: It looks like ISO-8859-2 doesn't support ñ. You may have to change something server-side. http://en.wikipedia.org/wiki/ISO/IEC_8859-2
You can try Html class. eg :-
jresponse = Html.fromHtml(jresponse);

Categories

Resources