I read many example and try it. But it didn't work to get the value.
The result returned a whole JSON to me.
I don't know where is wrong...
This is my first time to ask question. Thanks for help.
Here is my JSON and JAVA Code.
JSON:`
{
"food":{
"name":"XXX Food",
"details":{
"code":"01",
"location":{
"area":"Area D",
"place":"Food"
},
"price":"132.21",
"discount":"15%"
}
}
}
Now, I want to get the price.
JAVA Code:
JSONObject json = new JSONObject(recvStr);
JSONArray Results = json.getJSONArray("food");
JSONArray msg = Results.getJSONArray(0);
result = msg.getJSONObject(0).getString("price");
Results is not a JSONArray but a JSONObject:
JSONObject results = json.getJSONObject("food");
String name = results.getString("name");
JSONObject details = results.getJSONObject("details");
String price = details.getString("price");
You can read more about JSON at json.org.
Related
I'm new to this java and Gson (google JSON).
this is the JSON data i.m getting from server.it have array and object.
I have to De-serialize by google library (Gson)
{
"d":{
"results":[
{
"__metadata":{
"id":"employeeHRSet('00005011')",
"uri":employeeHRSet('00005011')",
"type":"SRV.employeeHR"
},
"Gender":"M",
"ImFname":"",
"ProfilePic":"user_image.png",
"ImLname":"",
"ImPernr":"00000000",
"ImSuper":"",
"ImUname":"",
"ExOk":"",
"Perno":"00005011",
"CompCode":"1000001013",
"Name":"Nishan Thevathosan",
"LastName":"Thevathosan",
"LastName2":"Nishan",
"Firstname":"Nishan",
"Userid":"GEES02",
"Email":"NISHAN#gamail.COM",
"Celno":"",
"Isexecutive":"Y",
"Designation":"80000000"
}
]
}
}
I need to get Firstname ,LastName ,Email ,Userid from this JSON data.
I'm in middle of a work i got struck in this please.
I have try some think like this but it didn't work.
JsonObject jsonObject = new JsonParser().parse(RESTRespArr).getAsJsonObject();
System.out.println("jsonObject working");
System.out.println(jsonObject.getAsString());
JsonElement d = jsonObject.get("d");
System.out.println("d working");
System.out.println(d.getAsString());
sonElement results = ((JsonObject) d).get("results");
System.out.println("results working");
System.out.println(results.getAsString());
System.out.println(((JsonObject) results).get("LastName").getAsString());
It's worked for me.
final JsonElement lastName = results.getAsJsonArray().get(0).getAsJsonObject().get("LastName");
results is JsonArray of JsonObjects, you cant cast JsonObject. So get first element of array and cast it to JsonObject with getAsJsonObject method. And get which field do you need.
In detail:
if JSON any value in curlybrackets { ... } , this is jsonObject . If values are in [ ... ], this is JsonArray. Also you can get which one is object or array, and get it relevant fields from this.
FIRST:
Your JSON data's format is incorrect on the seventh line:
modify <"uri":employeeHRSet('00005011')",> to <"uri":"employeeHRSet('00005011')",>
SECOND:
JsonObject jsonObject = new JsonParser().parse(RESTRespArr).getAsJsonObject();
System.out.println("jsonObject working");
System.out.println(jsonObject.toString());
JsonElement d = jsonObject.get("d");
System.out.println("d working");
System.out.println(d.toString());
JsonElement results = ((JsonObject) d).get("results");
System.out.println("results working");
System.out.println(results.toString());
System.out.println(results.getAsJsonArray().get(0).getAsJsonObject().get("LastName"));
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 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/
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'];
am a android application programmer and working on JSON right now, i have a format of json, shown in below. i am getting such format form server to my android program but the problem is am unable to show the result in listview.
could provide me the logic to read that json format.
{
"1":
{
"sub1":{"marks":"10",
"maxmarks":"60",
"grade":"D"
},
"sub2":{"marks":"",
"maxmarks":"60",
"grade":""
}
},
"2":
{
"sub3":{"marks":"30",
"maxmarks":"60",
"grade":"B"
},
"sub4":{"marks":"",
"maxmarks":"60",
"grade":""
}
}
}
Use the JSONObject in Android: http://developer.android.com/reference/org/json/JSONObject.html
Here is a good tutorial: http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/
For example:
JSONObject json = new JSONObject(<your json string>);
JSONObject objectOne = json.getJSONObject("1");
JSONObject subOne = objectOne.getJSONObject("sub1");
string marksOne = subOne.getString("marks");
string maxMarksOne = subOne.getString("maxmarks");
string gradeOne = subOne.getString("grade");
There is number of libraries that can help you with the task like Jackson or GSON
What info are you trying to show in your ListView? That will help us help you parse it.
For starters you can separate the data from the two objects using...
//Assuming your original object is named responseObj
JSONObject obj1 = responseObj.getJSONObject("1");
JSONObject obj2 = responseObj.getJSONObject("2");
//Now to get each piece of data
JSONObject s1 = obj1.getJSONObject("sub1");
string marks1 = s1.getString("marks");
string maxMarks1 = s1.getString("maxmarks");
string grade1 = s1.getString("grade");
JSONObject s3 = obj2.getJSONObject("sub3");
string marks3 = s3.getString("marks");
string maxMarks3 = s3.getString("maxmarks");
string grade3 = s3.getString("grade");
From here you have all of the data and can add them to a ListView in a TextView