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/
Related
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");
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.
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
My PHP code is this:
$userdetails = mysqli_query($con, "SELECT *FROM aircraft_status");
#$row = mysql_fetch_row($userdetails) ;
while($rows=mysqli_fetch_array($userdetails)){
$status[]= array($rows['Aircraft']=>$rows['Status']);
}
#Output the JSON data
echo json_encode($status);
and gives this:
[{"A70_870":"1"},{"A70_871":"1"},{"A70_872":"1"},{"A70_873":"1"},{"A70_874":"1"},{"A70_875":"1"},{"A70_876":"2"},{"A70_877":"1"},{"A70_878":"2"},{"A70_879":"2"},{"A70_880":"2"},{"A70_881":"0"},{"A70_882":"0"},{"A70_883":"0"},{"A70_884":"0"},{"A70_885":"0"}]
The java code that reads it is this:
// Create a JSON object from the request response
JSONObject jsonObject = new JSONObject(result);
//Retrieve the data from the JSON object
n870 = jsonObject.getInt("A70_870");
n871 = jsonObject.getInt("A70_871");
n872 = jsonObject.getInt("A70_872");
n873 = jsonObject.getInt("A70_873");
n874 = jsonObject.getInt("A70_874");
n875 = jsonObject.getInt("A70_875");
n876 = jsonObject.getInt("A70_876");
n877 = jsonObject.getInt("A70_877");
n878 = jsonObject.getInt("A70_878");
n879 = jsonObject.getInt("A70_879");
n880 = jsonObject.getInt("A70_880");
n881 = jsonObject.getInt("A70_881");
n882 = jsonObject.getInt("A70_882");
n883 = jsonObject.getInt("A70_883");
n884 = jsonObject.getInt("A70_884");
n885 = jsonObject.getInt("A70_885");
When i run my android app I seem to keep getting the error:
"of type org.json.JSONArray cannot be converted into Json object"
However when I send the app dummy code without the square brackets, it seems to work fine! How do I get rid of those [ and ] brackets on the ends???
Alternatively is there a way to accept the json as it is and adapt the java to read it?
echo json_encode($status, JSON_FORCE_OBJECT);
Demo: http://codepad.viper-7.com/lrYKv6
or
echo json_encode((Object) $status);
Demo; http://codepad.viper-7.com/RPtchU
Instead of using JSonobject, use JSONArray
JSONArray array = new JSONArray(sourceString);
Later loop through the array and do the business logic.
http://www.json.org/javadoc/org/json/JSONArray.html
Maybe with this kind of json structure?
$status[$rows['Aircraft']]= $rows['Status'];
You get an JSONArray, Not Object, you could create an Object holding an array, or parsing the array.
Refering to this post
Solution #1 (Java)
How about a helper method like this:
private int getProp(String name, JSONArray arr) throws Exception {
for (int i = 0; i < arr.length(); ++i) {
JSONObject obj = arr.getJSONObject(i);
if (obj.has(name))
return obj.getInt(name);
}
throw new Exception("Key not found");
}
Then you could use it like:
JSONArray jsonArray = new JSONArray(result); // note the *JSONArray* vs your *JSONObject*
n870 = getProp("A70_870", jsonArray);
n871 = getProp("A70_871", jsonArray);
...
Note I haven't tested this code, so you may need to make some changes...
Alternate solution (PHP)
It's been awhile since I've worked with PHP, but you might be able to leave your Java code intact and change your PHP int the while-loop body to:
$status[$rows['Aircraft']] = $rows['Status'];
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.