Before I get started, could I just point out I've spent the last 5 hours going in circles here. I've tried what seems like every StackOverflow method out there to pull what seems like a simple int from an API. This seems like it should be way easier, but isn't.
What I'm trying to achieve is pull the JSON info from this URL:
http://api.apixu.com/v1/current.json?key=ENTERKEYHERE&q=leeds
And then gather the category "temp_c", which should be something along the lines of "7.0". Below is an example of how a call looks:
{
"location": {
"name": "Leeds",
"region": "West Yorkshire",
"country": "United Kingdom",
"lat": 53.81,
"lon": -1.54,
"tz_id": "Europe/London",
"localtime_epoch": 1509670055,
"localtime": "2017-11-03 0:47"
},
"current": {
"last_updated_epoch": 1509669008,
"last_updated": "2017-11-03 00:30",
"temp_c": 7,
"temp_f": 44.6,
"is_day": 0,
"condition": {
"text": "Partly cloudy",
"icon": "//cdn.apixu.com/weather/64x64/night/116.png",
"code": 1003
},
"wind_mph": 4.3,
"wind_kph": 6.8,
"wind_degree": 150,
"wind_dir": "SSE",
"pressure_mb": 1015,
"pressure_in": 30.4,
"precip_mm": 0,
"precip_in": 0,
"humidity": 93,
"cloud": 75,
"feelslike_c": 5.8,
"feelslike_f": 42.4,
"vis_km": 10,
"vis_miles": 6
}
}
I've successfully gotten the data to pull into an app during random points in the night, however have been unable to pull the specific piece of data at all. Currently, my app won't pull it at all and I'm not sure what I did before.
I'm truly sorry if this is a question deemed "duplicate", but I can assure you that for someone trying to learn the language, none of the answers fully explain how to do this using the newer API's (Some of the answers seem to no longer work in API 23+)
I'm not looking for a detailed app with loads of methods, just a simple way of pulling the JSON data and selecting a specific category. I'd be extremely grateful if you could explain how to do this as I plan on adapting the code in the future for other sources. Thank you!
You should use an AsyncTask or AsyncTaskLoader so the UI-Thread doesn't lag during loading. Search for this term or use the UI thread, but then it will lag during the load. Eitherways, call this function, with url being your url:
public static String getResponse(URL url) throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = urlConnection.getInputStream();
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\A");
boolean hasInput = scanner.hasNext();
if (hasInput) {
return scanner.next();
} else {
return null;
}
} finally {
urlConnection.disconnect();
}
The url can be gotten by calling
url = new URL(yourAddressAsAString)
The upper functoin should return a string. Convert it into a JSON Object
JSONObject obj = null;
try {
obj = new JSONObject(yourString);
} catch (Throwable t) { }
Then call
String tempc = String.valueOf(obj.getJSONObject("current").getJSONObject("temp_c"))
To get the Object in question. Good luck!
First step take your whole Json as a String lets say String json_string and here is what you have to do (Read the comments in code for further information):
String json_string="your json string here as a normal String json object";
try {
JSONObject jsonObjectRoot=new JSONObject(json_string);
JSONObject locationObject=jsonObjectRoot.getJSONObject("location");
String name=locationObject.getString("name"); //This will return leeds
String region=locationObject.getString("region"); //This will return West YorkShire!
}catch (JSONException json_except){
//You have an exception so check if your items really exist with those names!
json_except.printStackTrace();
}
So from the above example using your own data you need to know is:
All Json code should be in a try block and catch Json exception.
To get a json object from a string just a constructor itself like new JSONObject(string);
To get any inner json object (json object within json object) we use the method outer_object.getJSONObject("string_name_of_inner_object");and continue deeper until you get your object.
To get a string within an object we use String string=json_object.getString("the_string_name_as_it_appears_in_the_object");
To get a Json Array inside an object you use JSONArray jsonArray=your_json_object.getJSONArray("array_name_as_it_appears_in_json");
I hope this will help you!
Related
I am trying to get a single nested value from a ResponseEntity but I am trying to do so without having to create a pojo for every possible item as this is a third party api response.
Example response.getBody() as it appears in Postman:
{
"message": "2 records found",
"records": [
{
"Account": {
"Id": "1",
"Name": "Foo Inc"
},
"CaseNumber": "200",
"Contact": {
"FirstName": "Foo",
"LastName": "Bar"
},
"Status": "In Progress",
"StatusMessage": "We are working on this."
},
{
"Account": {
"Id": "1",
"Name": "Foo Inc"
},
"CaseNumber": "100",
"Contact": {
"FirstName": "Foo",
"LastName": "Bar"
},
"Status": "Closed"
}
]
}
Basically, if I were in JS, I am looking for:
for(let record of res.body.records){
if(record && record.CaseNumber === "200"){
console.log(record.Status)
}
res.body.records[0].Status
Currently, they are are doing this to check if the response is empty:
ResponseEntity<Object> response = restTemplate.exchange(sfdcURL, HttpMethod.POST, entity, Object.class);
LinkedHashMap<Object, Object> resMap = (LinkedHashMap<Object, Object>) response.getBody();
List<Object> recordsList = (List<Object>) resMap.get("records");
if (recordsList.size() <= 0) { return error }
But I need to get the value of of "Status" and I need to do so without creating a pojo.
I appreciate any guidance on how I can do this in Java
UPDATE
So the response.getBody() is returned and when it is displayed in Postman, it looks like the pretty JSON shown above. However, when I do:
System.out.println(response.getBody().toString())
it looks like:
{message=2 Records Found, records=[{Account={Id=1, Name=Foo Inc}, CaseNumber=200, Contact={FirstName=Foo, LastName=Bar}, //etc
To make it worse, one of the fields appears in the console as follows (including linebreaks):
[...], Status=In Progress, LastEmail=From: noreply#blah.com
Sent: 2022-08-08 10:14:54
To: foo#bar.com
Subject: apropos case #200
Hello Foo,
We are working on your case and stuff
Thank you,
us, StatusMessage=We are working on this., OtherFields=blah, [...]
text.replaceAll("=", ":") would help some, but won't add quotations marks nor would it help separate that email block.
How can I so that the responses here like ObjectMapper and JSONObject can work?
You can either convert the string to valid json (not that trivial) and deserialise into a Map<String, Object>, or just pluck the value out of the raw string using regex:
String statusOfCaseNumber200 = response.getBody().toString()
.replaceAll(".*CaseNumber=200\\b.*?\\bStatus=([^,}]*).*", "$1");
This matches the whole string, captures the desired status value then replaces with the status, effectively "extracting" it.
The regex:
.*CaseNumber=200\b everything up to and including CaseNumber=200 (not matching longer numbers like 2001)
.*? as few chars as possible
\\bStatus= "Status=" without any preceding word chars
([^,}]*) non comma/curly brace characters
.* the rest
It's not bulletproof, but it will probably work for your use case so it doesn't need to be bulletproof.
Some test code:
String body = "{message=2 Records Found, records=[{Account={Id=1, Name=Foo Inc}, CaseNumber=200, Contact={FirstName=Foo, LastName=Bar}, Status=In Progress, StatusMessage=We are working on this.}, {Account={Id=1, Name=Foo Inc}, CaseNumber=100, Contact={FirstName=Foo, LastName=Bar}, Status=Closed}]";
String statusOfCaseNumber200 = body.replaceAll(".*CaseNumber=200\\b.*?\\bStatus=([^,}]*).*", "$1");
System.out.println(statusOfCaseNumber200); // "In Progress"
PLEASE DO NOT use Genson as Hiran showed in his example. The library hasn't been updated since 2019 and has many vulnerable dependencies!
Use Jackson or Gson.
Here how you can serialize a string into a Jackson JsonNode:
ObjectMapper mapper = new ObjectMapper();
String json = ...;
JsonNode node = mapper.readTree(json);
If you want to serialize a JSON object string into a Map:
ObjectMapper mapper = new ObjectMapper();
String json = ...;
Map<String, Object> map = mapper.readValue(json, HashMap.class);
You can read more about JsonNode here and a tutorial here.
You can use JSON-Java library and your code will look like this:
JSONObject jsonObject = new JSONObject(JSON_STRING);
String status = jsonObject.getJSONArray("records")
.getJSONObject(0)
.getString("Status");
System.out.println(status);
Or in a loop
JSONArray jsonArray = new JSONObject(jsonString).getJSONArray("records");
for(int i =0; i < jsonArray.length(); i++) {
String status = jsonArray
.getJSONObject(i)
.getString("Status");
System.out.println(status);
}
So the response.getBody() is returned and when it is displayed in Postman, it looks like the pretty JSON shown above. However, when I do:
...
text.replaceAll("=", ":") would help some, but won't add quotations
marks nor would it help separate that email block.
How can I so that the responses here like ObjectMapper and JSONObject
can work?
Firstly, Jackson is the default message converter which Spring Web uses under the hood to serialize and deserialize JSON. You don't need to introduce any dependencies.
Secondly, the process serialization/deserialization is handled by the framework automatically, so that in many cases you don't need to deal with the ObjectMapper yourself.
To emphasize, I'll repeat: in most of the cases in Spring you don't need to handle raw JSON yourself. And in the body of ResponseEntiry<Object> produced by the method RestTemplate.exchange() you have a LinkedHashMap in the guise of Object, it's not a raw JSON (if you want to know why it is a LinkedHashMap, well because that's how Jackson stores information, and it's a subclass of Object like any other class in Java). And sure, when you're invoking toString() on any implementation of the Map you'll get = between a Key and a Value.
So, the problem you've mentioned in the updated question is artificial.
If you want to deal with a Map instead of an object with properly typed properties and here's how you can do that:
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<LinkedHashMap<String, Object>> response = restTemplate.exchange(
sfdcURL, HttpMethod.POST, entity, new ParameterizedTypeReference<>() {}
);
Map<String, Object> resMap = response.getBody();
List<Object> recordsList = (List<Object>) resMap.get("records");
if (recordsList.isEmpty()) { ... }
If there are redundant lines in the Values which you want to trim, then as a remedy you can introduce a custom Jackson-module declaring a Deserializer which would handle leading/trailing white-space and new lines, described in this answer. Deserialize in the module would be applied by default, other options would require creating classes representing domain objects which you for some reasons want to avoid.
As Oliver suggested JsonNode seems to be the best approach. But, if I receive the ResponseEntity<Object>, I still cannot figure out a way to convert it to readable Json (and thus convert it to JsonNode), so I am still open to responses for that part.
I was able to get it to work by changing the ResponseEntity<Object> to ResponseEntity<JsonNode> so this is what I will be submitting for now:
ResponseEntity<JsonNode> response = restTemplate.exchange(sfdcURL,
HttpMethod.POST, entity, JsonNode.class);
JsonNode records = response.getBody().get("records");
String status = null;
String statusMessage = null;
for (JsonNode rec : records) {
if(rec.get("CaseNumber").asText().equals(caseNumber)) {
status = rec.get("Status").asText();
if(rec.has("StatusMessage")) {
statusMessage = rec.get("StatusMessage").asText();
}
} else {
statusMessage = "Invalid CaseNumber";
}
}
Because the overall method returns a ResponseEntity<Object> I then converted my strings to a HashMap and returned that:
HashMap<String, String> resMap = new HashMap<String, String>();
resMap.put("Status", status);
resMap.put("StatusMessage", statusMessage);
return new ResponseEntity<>(resMap, HttpStatus.OK);
This is not a perfect solution, but it works for now. Would still be better for exception handling if I could receive a ResponseEntity<Object> and then convert it to a JsonNode though. Thanks everyone for the responses!
I'm facing the problem for pagination data. Page data is getting calculated using the outer array dXPRecommendationResponses where I want it should be calculated through the nested array recommendations. I've mentioned the response data in result section.
I tried to change the page data calculation but it is getting calculated using the the data which we are passing in PageImpl() constructor.
I've used this approach to paginate the data which we are getting from third party API.
But it is getting calculated using the dxpRecommendationslist.
#This is the code through I need to generate the response
List<DXPRecommendationResponse> dxpRecommendationslist = new ArrayList<>();
List<DXPActivity> dxpActivities = getThirdResponse(pageable, correlationId,
dxpRecommendationslist, reservationGuestId, reservationId, nbxRecommendationRequest);
return new PageImpl<>(dxpRecommendationslist, pageable, dxpActivities.size());
#This method is used to get data from third party
public List<DXPActivity> getThirdResponse(final Pageable pageable, final String correlationId,
List<DXPRecommendationResponse> list, String reservationGuestId, String reservationId,
NBXRecommendationRequest nbxRecommendationRequest) {
List<DXPActivity> dxpActivities = new ArrayList<>();
NBXRecommendationResponse nbxRecommendationResponse = vVNBXRecommendationService
.getCalendarRecommendation(nbxRecommendationRequest, correlationId);
if (nbxRecommendationResponse != null) {
DXPRecommendationResponse dxpRecommendationResponse = new DXPRecommendationResponse();
dxpRecommendationResponse.setReservationGuestId(reservationGuestId);
dxpRecommendationResponse.setReservationNumber(reservationId);
dxpRecommendationResponse.setRecommendationType("CalendarType");
dxpRecommendationResponse.setDateTime(new Date());
populateActivities(dxpActivities, nbxRecommendationResponse);
List<DXPActivity> filteredList;
if (!CollectionUtils.isEmpty(dxpActivities)
&& dxpActivities.size() >= (pageable.getPageSize() * pageable.getPageNumber())) {
filteredList = Lists.partition(dxpActivities, pageable.getPageSize()).get(pageable.getPageNumber());
} else {
filteredList = new ArrayList<DXPActivity>();
}
dxpRecommendationResponse.setRecommendations(filteredList);
list.add(dxpRecommendationResponse);
}
return dxpActivities;
}
#This method is used to populate data
private void populateActivities(List<DXPActivity> dxpActivities,
NBXRecommendationResponse nbxRecommendationResponse) {
for (Activity activity :nbxRecommendationResponse.getCalendarRecommendation().getRecommendations().getActivities()) {
DXPActivity dxpActivity = new DXPActivity();
orikaMapper.map(activity, dxpActivity);
dxpActivities.add(dxpActivity);
}
}
The response data:
{
"_embedded": {
"dXPRecommendationResponses": [
{
"recommendationType": "CalendarType",
"reservationGuestId": "525dab66-1492-4908-a3bf-b5de558368e5",
"reservationNumber": "3a39f9ad-7e34-4bdb-91eb-b907fd6986c7",
"dateTime": "2019-08-19T14:38:18.413",
"recommendations": [
{
"productCode": "BIKE2006111000",
"activityName": "Bimini Bike Tour",
"recommendationId": "1565948843492_410387839_BIKE2006111000_cal",
"categoryCode": "DARING",
"activityStartTime": "2020-06-11T08:30:00.000",
"activityEndTime": "2020-06-11T11:30:00.000",
"activityDescription": "Bimini Bike Tour",
"sequence": 34.0,
"packageId": 103806,
"sourceId": "BIMINI BIKE",
"levelOfActivity": "EASY"
},
{
"productCode": "CUL2006110900",
"activityName": "Bimini Culinary Tour",
"recommendationId":"156594884349,
"categoryCode": "CULTURED",
"activityStartTime": "2020-06-11T07:30:00.000",
"activityEndTime": "2020-06-11T12:30:00.000",
"activityDescription": "Bimini Culinary Tour",
"sequence": 29.0,
"packageId": 103940,
"sourceId": "BIMINI CUL",
"levelOfActivity": "MODERATE"
}
]
}
]
},
"page": {
"size": 10,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
The problem here is implementation of PageImpl constructor and how the resulting total is being computed here. Below is code taken from the constructor:
this.total = pageable.toOptional().filter(it -> !content.isEmpty())//
.filter(it -> it.getOffset() + it.getPageSize() > total)//
.map(it -> it.getOffset() + content.size())//
.orElse(total);
The important line here is second one, as described in doc comment, it is insurance against inconsistensies. It checks if you are on the last page and if so, it will use third line to compute resulting total. Therefore totally omitting total given by you. If you want to rewrite this behavior you should implement your Page which uses only given total.
BUT, I don't think it is semantically correct. You are mixing together things which probably should not be mixed (page object of one type, but the actual pagination done on another). The list given as pageable always has one element and you are paging subelements which does not seem right, is there even need to paginate the subelements?
One of the possible resolutions would be to make pageable only subelements by specifying Page<DXPActivity> recommendations member directly inside DXPRecommendationResponse and returning non-pageable DXPRecommendationResponse. But again, it seems a bit off. It really depens on what you are trying to build here and what is the logic behind that.
I downloaded the "Favorites" app for Bluemix Cloudant. It was nice, until I realized that the "fields" were hard coded in the java files on the server. For example the ResourseServlet Object has this hard coded create function.
create(Database db, String id, String name, String value, Part part,
String fileName) throws IOException {
Accompanied by hard coded MAP builds
Map<String, Object> data = new HashMap<String, Object>();
data.put("name", name);
data.put("_id", id);
data.put("value", value);
data.put("creation_date", new Date().toString());
leading to a database save..
db.save(data);
This type of example is ok if you want all your fields from all your json files hard coded in the java code.
But that doesn't work for and I imagine most All the applications I work with return text files pre-formatted in a JSON format.
So I need to simply be able to have a function that can receive like a "FileInputStream" object or if I have to, I can use a BufferedReader and read the thing in to a String and try to save the object.
I really like the cloudant database, because it stores and retrieves JSON but now I have 1000's of JSON files I want to store there of different fields types and structures and cannot really hard code all those value pairs for every type.
Is there a cool library or function that if I already have a file, it just puts it in the database like
db.save(JSONurl);
where the JSONurl is just a url from the web that send back JSON?
thanks for your help. Been up till 3am for a week trying to find that method. (it took me days to figure out that the fields were hard coded in the server code.... whoa)
I realize I could write a loop and try to parse the JSON data myself and re-encode it, but it seems like there should be something simpler and available.
You can use the db.save(JsonObject) to accomplish what you need.
See the sample code below. I am passing a hardcoded JSON string as a sample test, but you can read the JSON string from a file or URL if you need. Also I used JsonObject and JsonParser from Google, they are available in the Cloudant "Favorites" sample.
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
String JSONString = new String ("{ \"hot\": { \"season\": \"summer\", \"weather\": \"usually warm and sunny\" }, \"cold\": { \"season\": \"winter\", \"weather\": \"usually cold and snowy\" }, \"sneezy\": { \"season\": \"spring\", \"weather\": \"cool with rain and sun\" },\"colorful\": {\"season\": \"autumn\",\"weather\": \"breezes\"} }");
JsonParser parser = new JsonParser();
JsonObject data = (JsonObject)parser.parse(JSONString);
db.save(data);
This will create the following document in Cloudant:
{
"_id": "9ec91813106c4aa69ad38e42b268e1f8",
"_rev": "1-1d8ab47557aaeb1ac342cd4f5f153f16",
"hot": {
"season": "summer",
"weather": "usually warm and sunny"
},
"cold": {
"season": "winter",
"weather": "usually cold and snowy"
},
"sneezy": {
"season": "spring",
"weather": "cool with rain and sun"
},
"colorful": {
"season": "autumn",
"weather": "breezes"
}
}
Thank you for the response on the JsonParser object. While I was doing more research, I figured out how to do something similar with a Gson Object from google where I build a HashMap from a Json formatted string or URL that returns JSON. I used a loop to build a string from an input file, but perhaps that step could be avoided, but it worked.
import com.google.gson.Gson;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.StringBuilder;
import java.net.URL;
...
URL myURL = new URL("http://... some url that returns JSON");
BufferedReader reader = new BufferedReader(new InputStreamReader(myURL.openStream()));
while (true) {
iLine=reader.readLine();
if (iLine == null) {
break;
} // end if
stringBuilder.append(iLine);
} // end while
reader.close();
HashMap<String, Object> myMap = gson.fromJson(stringBuilder.toString(), HashMap.class);
myMap.put("_id", id + "");
myMap.put("value", "Writing From URL Object String V2");
db.save(myMap);
...
I realize my questions have been asked a lot but I have spent a considerable amount of time scouring both SO and google trying to get a better understanding of this concept with no success. I've seen many different implementations, which is what leads me to get some advice about my specific situation.
MY OBJECTIVE
I need to perform a post request to a php file and the goal is to ultimately populate fields in a list activity with some of the json data.
HTTP POST RESPONSE
Here is the format of the response data I'm getting back from the server, which appears to be a JSON object of arrays(?).
{"expense":[{"cat_id_PK":237,"cat_name":"Name1","cat_amount":"100.00","is_recurring":0},
{"cat_id_PK":238,"cat_name":"Name2","cat_amount":"200.00","is_recurring":0},
{"cat_id_PK":239,"cat_name":"Name3","cat_amount":"300.00","is_recurring":0},
{"cat_id_PK":240,"cat_name":"Name4","cat_amount":"400.00","is_recurring":0}],
"expense_rec": [{"cat_id_PK":207,"cat_name":"Name5","cat_amount":"500.00","is_recurring":1}]}
FIRST QUESTION
The code below is what I'm using to read the response. Is this how I should be handling that? It seems weird to get a json encoded response and then change it to a string, only to try and access elements of a json object again. Am I on the wrong track here?
//This code is in the doInBackground method of my "sendPostRequest" async task.
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
stringBuilder.append(bufferedStrChunk);
}
//Returns string to onPostExecute()
return stringBuilder.toString();
SECOND QUESTION
I have another file called "PostResponse.java" that holds the following code I modeled after a tutorial online. I'm unsure of how to interact with this class from the onPostExecute method. How can I access say, the first item in the first object (something like in PHP where you could do: expense[0]['cat_name']). I've tried to do this various ways with no success. Here is the PostResponse.java class:
public class PostResponse {
public Integer cat_id_PK;
public String cat_name;
public BigDecimal cat_amount;
public Integer is_recurring;
public int getID() {
return this.cat_id_PK;
}
public void setID(int cat_id_PK){
this.cat_id_PK = cat_id_PK;
}
public String getName() {
return this.cat_name;
}
public void setName(String cat_name) {
this.cat_name = cat_name;
}
public BigDecimal getAmount() {
return this.cat_amount;
}
public void setAmount(BigDecimal cat_amount) {
this.cat_amount = cat_amount;
}
public int getRecurring() {
return this.is_recurring;
}
public void setRecurring(int is_recurring) {
this.is_recurring = is_recurring;
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("*** Categories ***");
sb.append("cat_id_PK="+getID()+"\n");
sb.append("cat_name="+getName()+"\n");
sb.append("cat_amount="+getAmount()+"\n");
sb.append("is_recurring="+getRecurring()+"\n");
return sb.toString();
}
}
and here is the content of my onPostExecute method:
protected void onPostExecute(String result) {
super.onPostExecute(result);
Gson gson = new Gson();
PostResponse response = gson.fromJson(result, PostResponse.class);
System.out.println(result);
}
Like I said originally, my ultimate goal is to populate these items to a list activity, but at this point I'd settle for just knowing how to get at specific elements. However, if anyone would like to include how to populate a list activity in their response, it would save me a lot more headaches, as nothing with java is coming easy for me!
FIRST QUESTION
The code below is what I'm using to read the response. Is this how I should be handling that? It seems weird to get a json encoded response and then change it to a string, only to try and access elements of a json object again. Am I on the wrong track here?
It's one way of handling the http response. A "json encoded response" is little more than a text-based response, so converting it into a string on the receiving end would make sense. That is, the json you receive isn't an 'object' as far as Java is concerned: it's just textual representation of an object (or a bunch of objects in your case), received as a stream of bytes.
That being said, you can potentially shorten your code by skipping the string(builder) part. Gson provides an alternative constructor that takes a Reader instance, for which you can suppy the BufferedReader in your code snippet.
As a side note: the conversion of textual json into Java objects is a potentially 'heavy' operation. As such, you'd best avoid doing it on the main/ui thread, so just move it into the doInBackground() method of your AsyncTask (and change types appropriately).
SECOND QUESTION
I have another file called "PostResponse.java" that holds the following code I modeled after a tutorial online. I'm unsure of how to interact with this class from the onPostExecute method. How can I access say, the first item in the first object (something like in PHP where you could do: expense[0]['cat_name']). I've tried to do this various ways with no success.
You're close, but if you look more closely to the json sample, you'll see that your PostResponse class is not a good match for it:
{
"expense": [
{
"cat_id_PK": 237,
"cat_name": "Name1",
"cat_amount": "100.00",
"is_recurring": 0
},
{
"cat_id_PK": 238,
"cat_name": "Name2",
"cat_amount": "200.00",
"is_recurring": 0
},
{
"cat_id_PK": 239,
"cat_name": "Name3",
"cat_amount": "300.00",
"is_recurring": 0
},
{
"cat_id_PK": 240,
"cat_name": "Name4",
"cat_amount": "400.00",
"is_recurring": 0
}
],
"expense_rec": [
{
"cat_id_PK": 207,
"cat_name": "Name5",
"cat_amount": "500.00",
"is_recurring": 1
}
]
}
Consider the more hierarchical formatting above. On the first level there are two (json) objects: expense and expense_rec (both contain 0...* elements, as the square brackets indicate). That means that whatever class you're going to be trying to map the json onto, should define these fields too. If you now look at your PostResponse class, it should become obvious that in its current form it in fact models one of the child objects of the aforementioned fields.
Basically, the classes to map the json onto, should look somewhat like this:
PostResponse:
public class PostResponse {
public ExpenseItem[] expense;
public ExpenseItem[] expense_rec;
// List<ExpenseItem> is also supported
// getters & setters
}
ExpenseItem:
public class ExpenseItem {
public Integer cat_id_PK;
public String cat_name;
public BigDecimal cat_amount;
public Integer is_recurring;
// getters & setters
}
With the model classes defined, try to let Gson work its magic again. If all goes well, you should be able to access the data in a way similar to what you're used to in PHP:
// map json to POJOs
PostResponse response = new Gson().fromJson(bufferedReader, PostResponse.class);
// retrieve the cat_name for the first item (assuming there is one)
String catName = response.getExpense()[0].getName();
... or any of the other fields through the getters defined in ExpenseItem.
Once you have this part working, it's going to be fairly straightforward to supply the array or list of expenses to an adapter (have a look at ArrayAdapter in the Android framenwork) and bind that adapter to a ListView.
The answer is yes, you will get the response in InputStream
For your second question check this out - jsonschema2pojo this can be helpful while creating models for your JSON data.
Then to use GSON
Gson gson = new Gson();
YourObj yourObj = (YourObj) gson.fromJson(result, YourObj.class);
The answer is yes.Response is received as InputSteam
protected void onPostExecute(String result)
{
super.onPostExecute(result);
Gson gson = new Gson();
PostResponse response = gson.fromJson(result, PostResponse.class);
System.out.println(result);
}
This code section mostly means that after an AsyncTask that get web Response and get the String format json response,this onPostExecute will be called with that Stringfied json.
Gson gson = new Gson();
Gson is a library supported by Google for android to deserialization into your class OBject.
gson.fromJson(result, PostResponse.class);
This method is the real process of deserialization. result is Stringfied json and the second is the Target class you want to deserialize into.
This will return a PostResponse Object and you can use it now.
For the json data (
{"expense":[{"cat_id_PK":237,"cat_name":"Name1","cat_amount":"100.00","is_recurring":0},
{"cat_id_PK":238,"cat_name":"Name2","cat_amount":"200.00","is_recurring":0},
{"cat_id_PK":239,"cat_name":"Name3","cat_amount":"300.00","is_recurring":0},
{"cat_id_PK":240,"cat_name":"Name4","cat_amount":"400.00","is_recurring":0}],
"expense_rec": [{"cat_id_PK":207,"cat_name":"Name5","cat_amount":"500.00","is_recurring":1}]}),
it contains two different arrays here, one is "expense" and another is "expense_rec". So if you want to populate these items to a list activity, you can try the follow methods.
protected void onPostExecute(String result) {
super.onPostExecute(result);
JSONObject jsonObject = new JSONObject(builder.toString());
Log.i(TAG, "jsonObject is : " + jsonObject.toString());
//this is the first array data
JSONArray jsonArray = jsonObject.getJSONArray("expense");
Log.i(TAG, "Array length is: " + jsonArray.length());
for(int i = 0; i < jsonArray.length(); i++){
JSONObject jsoObj = jsonArray.getJSONObject(i);
String name = jsoObj.getString("cat_name");
Log.i(TAG, "file name is: " + name);
}
//this is the second array data
jsonArray = jsonObject.getJSONArray("expense_rec");
for(int i = 0; i < jsonArray.length(); i++){
JSONObject jsoObj = jsonArray.getJSONObject(i);
String name = jsoObj.getString("cat_name");
Log.i(TAG, "file name is: " + name);
}
}
I am new to using the json-simple library in Java and I've been through both the encoding and decoding samples. Duplicating the encoding examples was fine, but I have not been able to get the decoding ones to work with mixed type JSON.
One of my problems is that there are too many classes in the library which are not properly documented, and for which I do not have the source (in order to be able to read through and understand their purpose). Consequently, I am struggling to understand how to use a lot of these classes.
After reading this example:
String jsonText = "{\"first\": 123, \"second\": [4, 5, 6], \"third\": 789}";
JSONParser parser = new JSONParser();
ContainerFactory containerFactory = new ContainerFactory(){
public List creatArrayContainer() {
return new LinkedList();
}
public Map createObjectContainer() {
return new LinkedHashMap();
}
};
try {
Map json = (Map)parser.parse(jsonText, containerFactory);
Iterator iter = json.entrySet().iterator();
System.out.println("==iterate result==");
while(iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
System.out.println(entry.getKey() + "=>" + entry.getValue());
}
System.out.println("==toJSONString()==");
System.out.println(JSONValue.toJSONString(json));
} catch(ParseException pe) {
System.out.println(pe);
}
from the json-simple official decoding tutorial, I tried to decode this JSON:
{
"stat":{
"sdr": "MAC address of FLYPORT",
"rcv": "ff:ff:ff:ff:ff:ff",
"time": "0000000000000",
"type": 0,
"subt": 0,
"argv": [
{"type": "6","val": "NetbiosName"},
{"type": "6","val": "MACaddrFlyport"},
{"type": "6","val": "FlyportModel"},
{"type": "1","val": id}
]
}
}
I am writing following code to decode:
String jsonString = "{\"stat\":{\"sdr\": \"aa:bb:cc:dd:ee:ff\",\"rcv\": \"aa:bb:cc:dd:ee:ff\",\"time\": \"UTC in millis\",\"type\": 1,\"subt\": 1,\"argv\": [{1,2},{2,3}]}}";
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject newJSON = jsonObject.getJSONObject("stat");
System.out.println(newJSON);
But it doesn't work. Infact I was not able to get the unmodified example working either, and the original authors have not explained their code.
What is the easiest way to decode this JSON as shown?
This is the best and easiest code:
public class test
{
public static void main(String str[])
{
String jsonString = "{\"stat\": { \"sdr\": \"aa:bb:cc:dd:ee:ff\", \"rcv\": \"aa:bb:cc:dd:ee:ff\", \"time\": \"UTC in millis\", \"type\": 1, \"subt\": 1, \"argv\": [{\"type\": 1, \"val\":\"stackoverflow\"}]}}";
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject newJSON = jsonObject.getJSONObject("stat");
System.out.println(newJSON.toString());
jsonObject = new JSONObject(newJSON.toString());
System.out.println(jsonObject.getString("rcv"));
System.out.println(jsonObject.getJSONArray("argv"));
}
}
The library definition of the json files are given here. And it is not same libraries as posted here, i.e. posted by you. What you had posted was simple json library I have used this library.
You can download the zip. And then create a package in your project with org.json as name. and paste all the downloaded codes there, and have fun.
I feel this to be the best and the most easiest JSON Decoding.
Well your jsonString is wrong.
String jsonString = "{\"stat\":{\"sdr\": \"aa:bb:cc:dd:ee:ff\",\"rcv\": \"aa:bb:cc:dd:ee:ff\",\"time\": \"UTC in millis\",\"type\": 1,\"subt\": 1,\"argv\": [{\"1\":2},{\"2\":3}]}}";
use this jsonString and if you use the same JSONParser and ContainerFactory in the example you will see that it will be encoded/decoded.
Additionally if you want to print your string after stat here it goes:
try{
Map json = (Map)parser.parse(jsonString, containerFactory);
Iterator iter = json.entrySet().iterator();
System.out.println("==iterate result==");
Object entry = json.get("stat");
System.out.println(entry);
}
And about the json libraries, there are a lot of them. Better you check this.
Instead of downloading separate java files as suggested by Veer, you could just add this JAR file to your package.
To add the jar file to your project in Eclipse, do the following:
Right click on your project, click Build Path > Configure Build Path
Goto Libraries tab > Add External JARs
Locate the JAR file and add
This is the JSON String we want to decode :
{
"stats": {
"sdr": "aa:bb:cc:dd:ee:ff",
"rcv": "aa:bb:cc:dd:ee:ff",
"time": "UTC in millis",
"type": 1,
"subt": 1,
"argv": [
{"1": 2},
{"2": 3}
]}
}
I store this string under the variable name "sJSON"
Now, this is how to decode it :)
// Creating a JSONObject from a String
JSONObject nodeRoot = new JSONObject(sJSON);
// Creating a sub-JSONObject from another JSONObject
JSONObject nodeStats = nodeRoot.getJSONObject("stats");
// Getting the value of a attribute in a JSONObject
String sSDR = nodeStats.getString("sdr");