So in Java, I have this as my json String:
public static void main(String[] args) {
String stringJson = "{\n" +
"nodes: {\n" +
"Random-key-Here: {\n" +
"name: \"PRO-cnt-elastic2-4-i-0a414518a5b67\",\n" +
"transport_address: \"172.18.7.104:9300\",\n" +
"host: \"189.88.7.884\",\n" +
"ip: \"188.88.8.884:8880\",\n" +
"roles: [\n" +
"\"ingest\"\n" +
"],\n" +
"attributes: {\n" +
"zone: \"us-east-1a\"\n" +
"},\n" +
"tasks: {\n" +
"Random-key-Here: {\n" +
"node: \"-2688888mRPYHywWA\",\n" +
"id: 37202060,\n" +
"type: \"transport\",\n" +
"action: \"indices:data/write/reindex\",\n" +
"status: {\n" +
"total: 4869544,\n" +
"updated: 13920,\n" +
"created: 3654080,\n" +
"deleted: 0\n" +
"},\n" +
"description: \"blaaa\",\n" +
"start_time_in_millis: 1596456902705,\n" +
"running_time_in_nanos: 647855785005,\n" +
"cancellable: true,\n" +
"headers: { }\n" +
"}\n" +
"}\n" +
"}\n" +
"}\n" +
"}";
JsonObject jo = new JsonParser().parse(stringJson).getAsJsonObject();
Set s = jo.get("nodes").getAsJsonObject().entrySet().stream().map(e ->
e.getValue().getAsJsonObject().get("tasks").collect(toSet());
}
Important: notice inside the Json some of the Keys are actually a random string that I couldn't know what it is in advance... - so I Wrote them as "Random-key-Here"
In short: My goal is to retrieve the number of this field, "total: 4869544".
Above that's what I've tried. I've managed to have a list that contains one object member of a set, and it's the "Random-key-Here" as the key, and the value is the rest of my nested json.
I thought if I would repeat the same logic as I did to overcome the first "Random-key-Here" thing, like I did with
.entrySet().stream().map(e ->
e.getValue().getAsJsonObject().get("tasks")
then I would get to another level below, so instead of "tasks" I wrote "status"
and I tought then I would be able to get the rest of the nested json which is easier from then on... but instead, nothing is returned when doing that:
jo.get("nodes").getAsJsonObject().entrySet()
.stream()
.map(e -> e.getValue().getAsJsonObject().get("tasks").getAsJsonObject().entrySet()
.stream()
.map(ent-> ent.getValue().getAsJsonObject().get("status").getAsJsonObject().get("total").getAsJsonObject()))
would appreciate any help to get to the nested part I need which is: "total: 4869544".
and keep in mind, I can't simply just use this:
jo.get("nodes").getAsJsonObject().get("Random-key-Here").getAsJsonObject().get("tasks")
.getAsJsonObject().get("Random-key-Here").getAsJsonObject()
.get("status").getAsJsonObject().get("total").getAsInt()
because of "Random-key-Here".
Tnx for the helpers!
Answer to myself: So, on every unknown key that you have, you can use entrySet()
and then loop over it until you get to your desired field member.
so this usage solves the problem and can get you the nested Json even when your key is unknown in advance:
jo.get("nodes").getAsJsonObject().entrySet()
.forEach(node -> node.getValue().getAsJsonObject().get("tasks").getAsJsonObject().entrySet()
.forEach(task -> { int i = task.getValue().getAsJsonObject().get("status").getAsJsonObject().get("total").getAsInt();
System.out.println(i); }))
pls share if someone has a more efficient way to do so
I receive a Http response after a call as Html String and I would like to scrape certain value stored inside the ReportViewer1 variable.
<html>
....................
...........
<script type="text/javascript">
var ReportViewer1 = new ReportViewer('ReportViewer1', 'ReportViewer1_ReportToolbar', 'ReportViewer1_ReportArea_WaitControl', 'ReportViewer1_ReportArea_ReportCell', 'ReportViewer1_ReportArea_PreviewFrame', 'ReportViewer1_ParametersAreaCell', 'ReportViewer1_ReportArea_ErrorControl', 'ReportViewer1_ReportArea_ErrorLabel', 'ReportViewer1_CP', '/app/Telerik.ReportViewer.axd', 'a90a0d41efa6429eadfefa42fc529de1', 'Percent', '100', '', 'ReportViewer1_EditorPlaceholder', 'ReportViewer1_CalendarFrame', 'ReportViewer1_ReportArea_DocumentMapCell', {
CurrentPageToolTip: 'STR_TELERIK_MSG_CUR_PAGE_TOOL_TIP',
ExportButtonText: 'Export',
ExportToolTip: 'Export',
ExportSelectFormatText: 'Export to the selected format',
FirstPageToolTip: 'First page',
LabelOf: 'of',
LastPageToolTip: 'Last Page',
ProcessingReportMessage: 'Generating report...',
NoPageToDisplay: 'No page to display.',
NextPageToolTip: 'Next page',
ParametersToolTip: 'Click to close parameters area|Click to open parameters area',
DocumentMapToolTip: 'Hide document map|Show document map',
PreviousPageToolTip: 'Previous page',
TogglePageLayoutToolTip: 'Switch to interactive view|Switch to print preview',
SessionHasExpiredError: 'Session has expired.',
SessionHasExpiredMessage: 'Please, refresh the page.',
PrintToolTip: 'Print',
RefreshToolTip: 'Refresh',
NavigateBackToolTip: 'Navigate back',
NavigateForwardToolTip: 'Navigate forward',
ReportParametersSelectAllText: '<select all>',
ReportParametersSelectAValueText: '<select a value>',
ReportParametersInvalidValueText: 'Invalid value.',
ReportParametersNoValueText: 'Value required.',
ReportParametersNullText: 'NULL',
ReportParametersPreviewButtonText: 'Preview',
ReportParametersFalseValueLabel: 'False',
ReportParametersInputDataError: 'Missing or invalid parameter value. Please input valid data for all parameters.',
ReportParametersTrueValueLabel: 'True',
MissingReportSource: 'The source of the report definition has not been specified.',
ZoomToPageWidth: 'Page Width',
ZoomToWholePage: 'Full Page'
}, 'ReportViewer1_ReportArea_ReportArea', 'ReportViewer1_ReportArea_SplitterCell', 'ReportViewer1_ReportArea_DocumentMapCell', true, true, 'PDF', 'ReportViewer1_RSID', true);
</script>
...................
...................
</html>
The value is a90a0d41efa6429eadfefa42fc529de1 and this is in the middle of this content:
'/app/Telerik.ReportViewer.axd', 'a90a0d41efa6429eadfefa42fc529de1', 'Percent', '100',
Whats the best way I can parse this value using Java?
Parse the HTML with String class
public class HtmlParser {
public static void main(String args[]){
String result = getValuesProp(html);
System.out.println("Result: "+ result);
}
static String PIVOT = "Telerik.ReportViewer.axd";
public static String getValuesProp(String json) {
String subString;
int i = json.indexOf(PIVOT);
i+= PIVOT.length();
//', chars
i+=2;
subString = json.substring(i);
i = subString.indexOf("'");
i++;
subString = subString.substring(i);
i = subString.indexOf("'");
subString = subString.substring(0,i);
return subString;
}
static String html ="<html>\n" +
"\n" +
"<script type=\"text/javascript\">\n" +
" var ReportViewer1 = new ReportViewer('ReportViewer1', 'ReportViewer1_ReportToolbar', 'ReportViewer1_ReportArea_WaitControl', 'ReportViewer1_ReportArea_ReportCell', 'ReportViewer1_ReportArea_PreviewFrame', 'ReportViewer1_ParametersAreaCell', 'ReportViewer1_ReportArea_ErrorControl', 'ReportViewer1_ReportArea_ErrorLabel', 'ReportViewer1_CP', '/app/Telerik.ReportViewer.axd', 'a90a0d41efa6429eadfefa42fc529de1', 'Percent', '100', '', 'ReportViewer1_EditorPlaceholder', 'ReportViewer1_CalendarFrame', 'ReportViewer1_ReportArea_DocumentMapCell', {\n" +
" CurrentPageToolTip: 'STR_TELERIK_MSG_CUR_PAGE_TOOL_TIP',\n" +
" ExportButtonText: 'Export',\n" +
" ExportToolTip: 'Export',\n" +
" ExportSelectFormatText: 'Export to the selected format',\n" +
" FirstPageToolTip: 'First page',\n" +
" LabelOf: 'of',\n" +
" LastPageToolTip: 'Last Page',\n" +
" ProcessingReportMessage: 'Generating report...',\n" +
" NoPageToDisplay: 'No page to display.',\n" +
" NextPageToolTip: 'Next page',\n" +
" ParametersToolTip: 'Click to close parameters area|Click to open parameters area',\n" +
" DocumentMapToolTip: 'Hide document map|Show document map',\n" +
" PreviousPageToolTip: 'Previous page',\n" +
" TogglePageLayoutToolTip: 'Switch to interactive view|Switch to print preview',\n" +
" SessionHasExpiredError: 'Session has expired.',\n" +
" SessionHasExpiredMessage: 'Please, refresh the page.',\n" +
" PrintToolTip: 'Print',\n" +
" RefreshToolTip: 'Refresh',\n" +
" NavigateBackToolTip: 'Navigate back',\n" +
" NavigateForwardToolTip: 'Navigate forward',\n" +
" ReportParametersSelectAllText: '<select all>',\n" +
" ReportParametersSelectAValueText: '<select a value>',\n" +
" ReportParametersInvalidValueText: 'Invalid value.',\n" +
" ReportParametersNoValueText: 'Value required.',\n" +
" ReportParametersNullText: 'NULL',\n" +
" ReportParametersPreviewButtonText: 'Preview',\n" +
" ReportParametersFalseValueLabel: 'False',\n" +
" ReportParametersInputDataError: 'Missing or invalid parameter value. Please input valid data for all parameters.',\n" +
" ReportParametersTrueValueLabel: 'True',\n" +
" MissingReportSource: 'The source of the report definition has not been specified.',\n" +
" ZoomToPageWidth: 'Page Width',\n" +
" ZoomToWholePage: 'Full Page'\n" +
" }, 'ReportViewer1_ReportArea_ReportArea', 'ReportViewer1_ReportArea_SplitterCell', 'ReportViewer1_ReportArea_DocumentMapCell', true, true, 'PDF', 'ReportViewer1_RSID', true);\n" +
" </script>\n" +
"\n" +
"</html>";
}
I would read the text a line at a time like how most files are read. Because the format will always be the same, you look for a line that begins with the characters "var ReportViewer1." Then you know you have found the line you want. You may need to strip some white space, although it will always be formatted with the same whitespace too (up to you really.)
When you have the line, use the String .split() method to split that line into an array. There are nice delimiters there to split on ... "," or " " or ", " ... again, see what works best for you.
Test the split up line parts for '/app/Telerik.ReportViewer.axd' ... the next member of your split array will be the value you are looking for.
Again, the formatting will always be the same, so you can rely on that to find your variable. Of course, study the html text to make sure it does always follow the same format within the line you are investigating, but looking at it, I assume it probably does.
Again, find your line ... split it on a delimiter ... and use some logic to find the element you are after in the split up line parts.
I'm not sure how to properly pass the multiple attributes needed for an OpenLDAP insert via UnboundID. I have omitted the objectClass attributes & received a "no objectClass" error. I have also tried comma-separated & the bracket/array route like below & received the "value #0 invalid per syntax" error.
String[] ldifLines = {"dn: ou=users,dc=sub,dc=domain,dc=com", "cn: " + uid, "userPassword: " + pw, "description: user", "uidNumber: " + lclDT, "gidNumber: 504", "uid: " + uid, "homeDirectory: " + File.separator + "home" + File.separator + this.getStrippedUser(), "objectClass: {posixAccount, top}"};
LDAPResult ldapResult = lclLC.add(new AddRequest(ldifLines));
So, the question is, how do I successfully pass these objectClass attributes in the string array included above? Again, I have tried: "objectClass: top, posixAccount" as well. Thanks in advance!
It uses an LDIF representation, so if an attribute has multiple values, then the attribute appears multiple times. Like:
String[] ldifLines =
{
"dn: ou=users,dc=sub,dc=domain,dc=com",
"objectClass: top",
"objectClass: posixAccount"
"cn: " + uid,
"userPassword: " + pw,
"description: user",
"uidNumber: " + lclDT,
"gidNumber: 504",
"uid: " + uid,
"homeDirectory: " + File.separator + "home" +
File.separator + this.getStrippedUser(),
};
LDAPResult ldapResult = lclLC.add(new AddRequest(ldifLines));
Also, the LDAP SDK allows you to use a shortcut and just do it in a single call without the need to create the array or the AddRequest object, like:
LDAPResult ldapResult = lclLC.add(
"dn: ou=users,dc=sub,dc=domain,dc=com",
"objectClass: top",
"objectClass: posixAccount"
"cn: " + uid,
"userPassword: " + pw,
"description: user",
"uidNumber: " + lclDT,
"gidNumber: 504",
"uid: " + uid,
"homeDirectory: " + File.separator + "home" +
File.separator + this.getStrippedUser());
Okay, I have a problem, I'm creating a launcher for Minecraft. My problem: I get this error messege, when I try launch Minecraft: Error: Could not find or load main class net.minecraft.client.main.Main
My code, I writted this, but not works:
The Detail of code:
Logger as module, my program using this to write information to console.
JavaPaht as string, the Java path is stored in this.
GameLibraries as string too, the libraries stored in this.
MinMemAlloc as string, the minimal allocated memory for java.
MaxMemAlloc as string, the maximal allocated memory for java.
Root as string, this is the root directory of Minecraft.
Here is the full sub to launch game:
Private Sub LaunchGame()
If Not File.Exists(Root + "\versions\" + SelectedGameVersion + "\" + SelectedGameVersion + ".jar") Then
MsgBox("File not found: " + SelectedGameVersion + ".jar")
Else
Logger.Write("Launching Game...")
Logger.SetScrollDown()
Dim Gamelibraries As String = Nothing
For i = 0 To FileList.Count - 1
Gamelibraries += FileList.Item(i) + ";" +
Environment.NewLine()
Next
Logger.WriteWithJumpDown("Libraries loaded: " & Gamelibraries.ToString())
Logger.SetScrollDown()
Logger.Write("Building Process...")
Logger.Write("Received data: ")
Logger.SetScrollDown()
Dim p As New Process()
p.StartInfo.FileName = JavaPath
p.StartInfo.Arguments = " -Xms" + MinMemAlloc + "M -Xmx" + MaxMemAlloc + "M " +
"-Djava.library.path=" + Root + "\versions\" + SelectedGameVersion + "\" + SelectedGameVersion + "-natives -cp " +
Gamelibraries.ToString() +
Root + "\versions\" + SelectedGameVersion + "\" + SelectedGameVersion + ".jar " + mainClass +
" --username=" + UserID +
" --version " + SelectedGameVersion +
" --gameDir " + Root +
" --assetsDir " + Root + "\assets" +
" --assetIndex " + assets +
" --accessToken null" +
" --userProperties {}" +
" --userType mojang" +
" --uuid (Default)"
p.StartInfo.WorkingDirectory = Root
p.StartInfo.CreateNoWindow = False
p.StartInfo.UseShellExecute = False
p.EnableRaisingEvents = True
Application.DoEvents()
p.StartInfo.RedirectStandardError = True
p.StartInfo.RedirectStandardOutput = True
AddHandler p.ErrorDataReceived, AddressOf p_OutputDataReceived
AddHandler p.OutputDataReceived, AddressOf p_OutputDataReceived
p.Start()
p.BeginErrorReadLine()
p.BeginOutputReadLine()
Logger.SetScrollDown()
Button1.Text = "Play"
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True
Button5.Enabled = True
End If
End Sub
And my program writing the output into my console, so I write here the output:
[14:34:41 INFO ] Libraries loaded: C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\java3d\vecmath\1.5.2\vecmath-1.5.2.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\net\sf\trove4j\trove4j\3.0.3\trove4j-3.0.3.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\ibm\icu\icu4j-core-mojang\51.2\icu4j-core-mojang-51.2.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\net\sf\jopt-simple\jopt-simple\4.6\jopt-simple-4.6.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\paulscode\codecjorbis\20101023\codecjorbis-20101023.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\paulscode\codecwav\20101023\codecwav-20101023.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\paulscode\libraryjavasound\20101123\libraryjavasound-20101123.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\paulscode\librarylwjglopenal\20100824\librarylwjglopenal-20100824.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\paulscode\soundsystem\20120107\soundsystem-20120107.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\io\netty\netty-all\4.0.15.Final\netty-all-4.0.15.Final.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\google\guava\guava\17.0\guava-17.0.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\apache\commons\commons-lang3\3.3.2\commons-lang3-3.3.2.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\commons-io\commons-io\2.4\commons-io-2.4.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\commons-codec\commons-codec\1.9\commons-codec-1.9.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\net\java\jinput\jinput\2.0.5\jinput-2.0.5.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\net\java\jutils\jutils\1.0.0\jutils-1.0.0.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\google\code\gson\gson\2.2.4\gson-2.2.4.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\mojang\authlib\1.5.16\authlib-1.5.16.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\com\mojang\realms\1.5\realms-1.5.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\apache\commons\commons-compress\1.8.1\commons-compress-1.8.1.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\apache\httpcomponents\httpclient\4.3.3\httpclient-4.3.3.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\apache\httpcomponents\httpcore\4.3.2\httpcore-4.3.2.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\apache\logging\log4j\log4j-api\2.0-beta9\log4j-api-2.0-beta9.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\apache\logging\log4j\log4j-core\2.0-beta9\log4j-core-2.0-beta9.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\lwjgl\lwjgl\lwjgl\2.9.1\lwjgl-2.9.1.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\lwjgl\lwjgl\lwjgl_util\2.9.1\lwjgl_util-2.9.1.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\org\lwjgl\lwjgl\lwjgl-platform\2.9.1\lwjgl-platform-2.9.1-natives-windows.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\net\java\jinput\jinput-platform\2.0.5\jinput-platform-2.0.5-natives-windows.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\tv\twitch\twitch\6.5\twitch-6.5.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\tv\twitch\twitch-platform\6.5\twitch-platform-6.5-natives-windows-64.jar;
C:\Users\ProGamer\AppData\Roaming\.elcplatform\libraries\tv\twitch\twitch-external-platform\4.5\twitch-external-platform-4.5-natives-windows-64.jar;
[14:34:41 INFO ] Building Process...
[14:34:41 INFO ] Received data:
[14:34:41 INFO ]
[14:34:41 INFO ] Error: Could not find or load main class net.minecraft.client.main.Main
[14:34:41 INFO ]
Thanks for help!
You need to have the main class on the path after the jars are included "net.minecraft.client.main.Main" unless mainClass means that already, then I don't know.
We call a webservice from our C# app which takes about 300ms using WCF (BasicHttpBinding). We noticed that the same SOAP call does only take about 30ms when sending it from SOAP UI.
Now we also implemented a test accessing the webservice via a basic WebClient in order to make sure that the DeSer-part of the WCf is not the reason for this additional delay. When using the WebClient class the call takes about 300ms as well.
Any ideas on why Java compared to C# is about 10x faster in this regard? Is there some kind of tweaking possible on the .NET side of things?
private void Button_Click(object sender, RoutedEventArgs e)
{
executeTest(() =>
{
var resultObj = client.getNextSeqNr(new WcfClient()
{
domain = "?",
hostname = "?",
ipaddress = "?",
loginVersion = "?",
processId = "?",
program = "?",
userId = "?",
userIdPw = "?",
userName = "?"
}, "?", "?");
});
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
WebClient webClient = new WebClient();
executeTest(()=>
{
webClient.Proxy = null;
webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
webClient.Headers.Add("Content-Type", "application/xml");
webClient.Encoding = Encoding.UTF8;
var data = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"SomeNamespace\">" +
" <soapenv:Header/>" +
" <soapenv:Body>" +
" <ser:getNextSeqNr>" +
" <!--Optional:-->" +
" <clientInfo>" +
" <!--Optional:-->" +
" <domain>?</domain>" +
" <!--Optional:-->" +
" <hostname>?</hostname>" +
" <!--Optional:-->" +
" <ipaddress>?</ipaddress>" +
" <!--Optional:-->" +
" <loginVersion>?</loginVersion>" +
" <!--Optional:-->" +
" <processId>?</processId>" +
" <!--Optional:-->" +
" <program>?</program>" +
" <!--Optional:-->" +
" <userId>*</userId>" +
" <!--Optional:-->" +
" <userIdPw>?</userIdPw>" +
" <!--Optional:-->" +
" <userName>?</userName>" +
" </clientInfo>" +
" <!--Optional:-->" +
" <name>?</name>" +
" <!--Optional:-->" +
" <schema>?</schema>" +
" </ser:getNextSeqNr>" +
" </soapenv:Body>" +
"</soapenv:Envelope>";
string result = webClient.UploadString("http://server:8080/service", "POST", data);
});
}
Am I missing something here? Any idea would be helpful... ;-)
Kind regards,
Sebastian
I just found the reason for this.
It's the 100-Expect Continue HTTP Header and the corresponding implementation in .NET. The .NET client wait 350ms as default on the server. This causes the delays. Java seems to have other default values here...
Just add the following line of code very early in your code:
System.Net.ServicePointManager.Expect100Continue = false;
Cheers!