Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have the following environment variable:
$ printenv
...
VCAP_SERVICES={"mariadbent":[{
"label": "mariadbent",
"provider": null,
"plan": "usage",
"name": "stackoverflow-database",
"tags": [
"mariadb",
"mysql"
],
"instance_name": "stackoverflow-database",
"binding_name": null,
"credentials": {
"host": "some-url-to-the-database.service",
"hostname": "some-url-to-the-database.service",
"port": 7689,
"name": "JDFJHDJF_DFJKDHFUD_DFUZDKFJDKJF",
"database": "JDFJHDJF_DFJKDHFUD_DFUZDKFJDKJF",
"username": "hsdfhsjkfhsjkhfjk",
"password": "iuzwerhsdjkfjkasd",
"database_uri": "mysql://dfdfdfdfdf:jrb4j4QxzgbAcfLk#some-url-to-the-database.service:3306/JDFJHDJF_DFJKDHFUD_DFUZDKFJDKJF?reconnect=true",
"uri": "mysql://dfdfdfdfdf:jrb4j4QxzgbAcfLk#some-url-to-the-database.service:3306/JDFJHDJF_DFJKDHFUD_DFUZDKFJDKJF?reconnect=true",
"jdbcUrl": "jdbc:mysql://some-url-to-the-database.service:3306/JDFJHDJF_DFJKDHFUD_DFUZDKFJDKJF?user=dfdfdfdfdf&password=jrb4j4QxzgbAcfLk"
},
"syslog_drain_url": null,
"volume_mounts": [
]
}]}
I can get the whole "pack" of data with System.out.println("VCAP_SERVICES: " + System.getenv("VCAP_SERVICES"));, but I would like to extract some field in the above output, like the username.
How could I do that?
Your VCAP_SERVICE hold an json. You can use an json parser to get a value from it.
Here is an example using Jackson, but there a more libs which can do this.
try{
String json = System.getenv("VCAP_SERVICES"); //NullPointerException, SecurityException
JsonNode jsonNode = (new ObjectMapper()).readTree(json); //IOException
if(jsonNode.has("mariadbent") && jsonNode.get("mariadbent").isArray()){
for(JsonNode elem : jsonNode.get("mariadbent")){
if(elem.has("credentials")){
JsonNode cred = elem.get("credentials");
if(cred.has("host")){
System.out.println(
cred.get("host").asText() //some-url-to-the-database.service
);
}else{ System.out.println("no host"); }
}else{ System.out.println("no credentials"); }
}
}else{ System.out.println("no mariadbent or not array"); }
}catch(Exception e){ e.printStackTrace(); }
For this you need lib Jackson Databind: https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.9.9.3
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 days ago.
Improve this question
We have 2 APIS one to generate SMS other to retrieve and post content of SMS.
http://54.68.219.97:8086//TigoWapPromotion/OmanTelServlet?product=29&service=27465&PID=test
To generate OTP
http://54.68.219.97:8086/IntegrationMServices/Generate_OTP/api/{msisdn}/{PromoID}/{PartnerID}/{TransactionID}
Response:
SUCCESS -
{
"Code": 0,
"Message": "Success",
"TransactionID": "190822141841961ZOTHERZ209Z0Z29224"
}
OR ERROR -
{
"Code": 99,
"Message": "System Error",
"TransactionID": ""
}
To Validate OTP
http://54.68.219.97:8086/IntegrationMServices/OTPValidate/api/{msisdn}/{OTP}/{TransactionID}
Response:
SUCCESS -
{
"Code": 0,
"Message": "Success",
"TransactionID": "190822141841961ZOTHERZ209Z0Z29224"
}
OR ERROR -
{
"Code": 1,
"Message": "PIN Error",
"TransactionID": ""
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Now, I want to create Gridview with section and I do following
this
and then I stuck problem at I don't know how to get position section and item from JSONArray
I have JSONArray like this
“Team”: [
{
"team_id": "16",
"team_name": "3",
"team_max_player": "6",
"team_amount": "2",
"team_member_list": [
{
"id": "19",
"room_id": "23",
"user_id": "75",
"team_id": "16",
"detail_status": ""
},
{
"id": "21",
"room_id": "23",
"user_id": "46",
"team_id": "16",
"detail_status": ""
}
]
},
{
"team_id": "14",
"team_name": "1",
"team_max_player": "1",
"team_amount": "1",
"team_member_list": [
{
"id": "20",
"room_id": "23",
"user_id": "40",
"team_id": "14",
"detail_status": ""
}
]
}
]
I want to set "team_id" as section and "team_member_list" as an item in that section.
please teach me to do it.
I am currently in grade 12 in school with programing class, android studio, JAVA Moblie App. Please help me and sorry about my English.
First you can load the JSON as a string and parse the JSON string using the JSONObject class.
You can then use the JSONArray class to parse the JSON Array inside your JSONObject to retrieve the details you want.
See the code sample below that will log each team member's id along with the id of which team they belong to in the console.
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray teams = jsonObject.getJSONArray("Team");
for (int i = 0; i < teams.length(); i++) {
String teamId = jsonObject.getJSONArray("Team").getJSONObject(i).getString("team_id");
JSONArray teamMemberList = jsonObject.getJSONArray("Team").getJSONObject(i).getJSONArray("team_member_list");
for (int j = 0; j < teamMemberList.length(); j++) {
String teamMemberId = teamMemberList.getJSONObject(j).getString("id");
Log.i("team", "team id: " + teamId + " team member id: " + teamMemberId);
}
}
If you would like to learn more about JSON parsing in Android, I wrote a JSON tutorial for Android that goes into much greater detail.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I`m newbie with elasticsearch querybuilder, Could someone give a constructed query for this below one in Java API
curl -XGET "http://localhost:9200/mone/mone/_search?pretty=true" -d'
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "ABC",
"fields": ["Data.Type"]
}
},
"filter": {
"term": { "Data.Date": "01.06.2014" }
}
}
}
}'
Using FilterQueryBuilder I got it to work
FilteredQueryBuilder builder = QueryBuilders.filteredQuery(QueryBuilders.queryString("Spectra"), FilterBuilders.termFilter("Data.Date", "01.06.2014"));
SearchResponse response = elasticClient.prepareSearch("mone")
.setTypes("mone")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(builder)
.execute()
.actionGet();
System.out.println(response);
Hope this answer will be useful to some newbies like me.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to write a program which can create an output in JSON format, how would be best way of doing this? and programming languages?
This is an example output of JSON (expected output) which I need to input in the Name, Gender, Qualification and other attributes in a user friendly way during the execution of script. And in which outputs in following JSON format. Sorry, I am new in programming, but so much interested to learn Perl (or) Python (or) Java. What could be the best here?
Any suggestions?
P.S Sorry I am quite new to JSON as well, please apologize me for this basic one.
[
{
"Name":"Steven Mark",
"gender":"male",
"Qualification": {
"college":"Bachelor in Science",
"tech":"certified pro"
},
"contributions": [
{
"name":"biography",
"type":"book",
},
]
},
{
"Name":"Andrea Mark",
"Gender":"female",
"Qualifications": {
"college":"Bachelor in physics",
},
"contributions": [
{
"name":"my insights",
"type":"movie",
},
]
}
]
Virtually every language has a JSON library, including Perl.
use JSON;
my $data = [
{
"Name" => "Steven Mark",
"gender" => "male",
"Qualification" => {
"college" => "Bachelor in Science",
"tech" => "certified pro"
},
"contributions" => [
{
"name" => "biography",
"type" => "book",
},
]
},
{
"Name" => "Andrea Mark",
"Gender" => "female",
"Qualifications" => {
"college" => "Bachelor in physics",
},
"contributions" => [
{
"name" => "my insights",
"type" => "movie",
},
]
}
];
print(encode_json($data));
If you agree to use ANY programming language, i can suggest python. With its json lib you can do following (lines with # is comments):
# import lib
import json
# fill data into variable (this is list with dict objects inside):
data = [{"name":"john"},{"name": "bill"}]
# dump json
json.dumps(data)
Which will output your data as json.
You can start writing python using something from https://wiki.python.org/moin/BeginnersGuide
If you are going to use Python, you can try to use simplejson or json module to create a json object.
For example,
try:
import simplejson
except:
import json
data = dict(a=1,b=2)
with open("results.json", "w") as fp:
json.dump(data, fp, indent=3, encoding="utf-8")
For dumping, json is faster than simplejson (but not by an order of magnitude). For loading, simplejson is faster (but not by an order of magnitude).
You can check here for more detailed comparison between simplejson and json.
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 9 years ago.
Iam new to java script and JSON, please help me in solving my problem. Below is the structure of my JSON in JavaScript
{
"name": "sample",
"def": [
{
"setId": 1,
"setDef": [
{
"name": "ABC",
"type": "STRING"
},
{
"name": "XYZ",
"type": "STRING"
}
]
},
{
"setId": 2,
"setDef": [
{
"name": "abc",
"type": "STRING"
},
{
"name": "xyz",
"type": "STRING"
}
]
}
]
}
in the backend, what should be the synatx of java method to receive this data
public void getJsonData(****){
}
How to parse this JSON data in java and what should be the syntax of method parameter ?
update 1: Edited the json format to make it valid
First create a class that will map your json object and give a name something like "DataObject". Then use the gson library and do the following:
String s = "";
DataObject obj = gson.fromJson(s, DataObject.class);
Your JSON is invalid, but assuming you fix that then you are looking for a library in Java which will serialize an annotated Java class to JSON, or deserialize JSON data to an annotated Java class.
There is a whole list of suitable libraries here:
http://json.org/java/