I am trying to get json data from a fake api call and need to get the count of the items in it(so that in future I can call the actual restful service). I am not able to get the number of departments in the json. I am expecting the result as 4(int) here.
I am not able to get the string value(json) for the code below:
String json = client.target("file:///C:/Program%20Files%20(x86)/apache-tomcat-8.0.35/webapps/GetProducts.json").request(MediaType.TEXT_PLAIN).get(String.class);
Please find below the entire code:
String json = client.target("file:///C:/Program%20Files%20(x86)/apache-tomcat-8.0.35/webapps/GetProducts.json").request(MediaType.TEXT_PLAIN).get(String.class);
JSONObject jsnobject = new JSONObject(json);
JSONArray jsonArray = jsnobject.getJSONArray("locations");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject explrObject = jsonArray.getJSONObject(i);
}
JSON Sample:
{
"Department":
[
{"SectionId":"1","SectionName":"Childrens Wear"},
{"SectionId":"2","SectionName":"Womens Wear"},
{"SectionId":"3","SectionName":"F&A"},
{"SectionId":"1","SectionName":"Mens Wear"}
]
}
I am new to java as well as api's.
Thanks,
You are either using incorrect key in code or you posted incorrect JSON example. You used locations as the key incode however there is no value against that key in sample JSON. You need to use Department as the key.
JSONArray jsonArray = jsnobject.getJSONArray("Department");
Related
I'm trying to get the data of this Json. As you can see the first element have a json inside of the json.
{
"client":{
"colour":"aabb11",
"height":200,
"xpos":0,
"packages":"com.samsung.incallUi",
"events":[
{
"action":"hide",
"class":"com.android.TextView",
"type":"2"
},
{
"colour":"00FF00",
"action":"show"
}
],
"width":600,
"ypos":20
},
"Map":" {red=blue, yellow=brown}",
"Country":"IT"
}
I get the json from:
Request request = new Request.Builder().url(INIT_URL).post(formBody).build();
Response response = client.newCall(request).execute();
I'm trying this, but don't work (give me an JsonSyntaxException):
...
JSONArray ja = new JSONArray(response.body().string());
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = ja.getJSONObject(i);
String client = jo.getString("client");
String xpos = jo.getString("height");
String packages = jo.getString("xpos");
String events = jo.getString("packages");
...
}
...
The first element, "client" is an object not an array.
try
JSONObject ja= new JSONObject(response.body().string());
I recommend you to take a look into a JSON parser, such us Jackson or Gson.
Will do your life much easier.
//Jackson example
Client client = new ObjectMapper().readValue(jsonString, Client.class);
Just define your Client class with the fields in the JSON
I strongly recommend using Gson. If you can't get it right with gson, that means, your model is not correct. If you have a hard time creating the model for the json, you can auto generate the model instead of creating it your self.
I am trying to extract a person's details who liked a facebook page by passing the page id as parameter. I extracted the JSON content of that page and now from that I want to extract name and id of users.
How do I achieve that ?
Code:
JSONObject json = readurl("https://graph.facebook.com/pageid");
System.out.println(json.toString());
System.out.println("Page id is:"+json.get("id"));
JSON:
"likes":{
"data":[
{
"id":"*******",
"name":"vv"
},
{
"id":"********",
"name":"abc"
},
Code like this would do the trick.
JSONObject json = readurl("https://graph.facebook.com/pageid");
JSONArray dataJsonArray = json.getJSONArray("data");
for(int i=0; i<dataJsonArray.length; i++) {
JSONObject dataObj = dataJsonArray.get(i);
String id = dataObj.getString("id");
//Similarly you can extract for other fields.
}
Basically data is a JSONArray since it starts with [. So simply get would not work, you must use JSONArray.
Note: I haven't compiled this code, but I think I gave you idea to proceed. Also refer this link to get hold of basics of parsing JSON in java.
This snippet is not tested, but I'm pretty sure it works:
JSONArray data = json.getJSONArray("data");
for (int i=0; i < data.length(); i++) {
JSONObject o = data.getJSONObject(i);
sysout(o.getString("id");
sysout(o.getString("name");
}
I use google's Gson library from: https://code.google.com/p/google-gson/
In my android project, I have an activity in which I want to obtain data from database using a PHP script. I manage the result of the script in this line :
String result = EntityUtils.toString(entity);
I created the jsonObject :
JSONObject jsonObject = new JSONObject(result);
I print this line and get: {"id":"1"}{"id":"2"}{"id":"3"}
But when I do this:
int i;
for(i=0;i<array.length;i++)
{
array[i] = "ID : "+jsonObject.getString("id");
}
I obtain "id : 1" three times, so I think there are some errors in the cycle..
the code of script php is here :
#Get the first row of the results
while ($row = mysqli_fetch_row($data)) {
#Build the result array (Assign keys to the values)
$result_data = array(
'id' =>$row[0],
);
#Output the JSON data
echo json_encode($result_data);
change to:
int i;
for(i=0;i<array.length;i++)
{
jsonObject=array[i];
String s = "ID : "+jsonObject.getString("id");
}
{"id":"1"}{"id":"2"}{"id":"3"}
is no valid code for a single JSON object - see here: JSON syntax.
I can only guess what you try to achieve, but I would guess your intention is to have a JSON array with 3 objects, each having an "id" value. The JSON code for such a structure should look like this:
[{"id":"1"},{"id":"2"},{"id":"3"}]
If you can make "EntityUtils.toString(entity)" to return the above JSON code, the following loop should also work:
JSONArray ja = new JSONArray(result);
for (int i = 0; i < ja.length(); i++) {
JSONObject jsonObject = ja.getJSONObject(i);
System.out.println("ID : "+jsonObject.getString("id"));
}
edit
On a side note: I believe you get the result you describe, because when you call
new JSONObject(result);
where the result is a String that consists of
{"id":"1"}{"id":"2"}{"id":"3"}
then most likely JSONObject stops parsing the JSON code after the first right brace without throwing a parse exception. So it actually only parses the first JSON object and because of this you get "id : 1" three times. Personally I would consider this behavior a bug, so consider reporting it.
the string I have into "jsonString" is the content of this link: http://85.18.173.82/cineca/wp5/json/events.json
Now I want the value "Day" of the second "Event".
JSONObject o = new JSONObject(jsonString);
String day = o.getString("XXXXXXXXXX");
System.out.println(day);
What does I have to put as argument of o.getString?
Many thanks
JSONObject obj = new JSONObject(json);
JSONArray array = obj.getJSONArray("Events");
for(int i = 0 ; i < array.length() ; i++){
System.out.println(array.getJSONObject(i).getJSONObject("Event").getString("Day"));
}
In this way, you can access, thanks.
The way you're constructing your JSONObject is wrong. By using this constructor you're not reading the json from that URL, you're actually using that string as a json representation (which it is not).
If you want to first read the json from your URL you'll have to do an HTTP GET request and then construct a JSONObject out of the response.
For more info, take a look at JSONObject docs
hello
i have an some 10 datas from db with attribute same attribute name
JSONObject json = new JSONObject();
json.put("text",value1);
json.put("title",value2);
json.put("url",value3);
i use this above code i am getting similar to this
{"text":"texting is not bad","title":"tesing","url":"http:\/\/www.example.com\/"}
{"text":"texting is not bad","title":"tesing","url":"http:\/\/www.example.com\/"}
{"text":"texting is not bad","title":"tesing","url":"http:\/\/www.example.com\/"}
{"text":"texting is not bad","title":"tesing","url":"http:\/\/www.example.com\/"}
while i parse it in php i am getting an null value i dont know y .. can you tell me where i am wrong...
your output should be something like this to be parsable
[{"text":"texting is not bad","title":"tesing","url":"http:\/\/www.example.com\/"},
{"text":"texting is not bad","title":"tesing","url":"http:\/\/www.example.com\/"},
{"text":"texting is not bad","title":"tesing","url":"http:\/\/www.example.com\/"},
{"text":"texting is not bad","title":"tesing","url":"http:\/\/www.example.com\/"}]
and for this you should be using JSONArray.
Basically you will have to create a JSON Array to hold your 10 datas, like so:
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < 10; i++) {
JSONObject json = new JSONObject();
json.put("text",value1);
json.put("title",value2);
json.put("url",value3);
jsonArray.put(json);
}
Output, See Bala R's response.