I am getting json response in this format.
{
"apiGroups":
{
"Affiliate":
{
"listingsAvailable":
{
"Beauty_Personal_Care":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:586:821655440?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Eyewear":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:473:662748456716?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Real_Estate":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:897:673143570606?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Jewellery":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:6:315773046?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Furniture":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:580:1894930153?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Tweens_Boys":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:814:934253466?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Automobiles":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:1145:639299259208?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Home_Improvement":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:864:624389489778?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"The_Designer_Studio":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:924:655684426383?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Fashion_Jewellery":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:1113:672114192240?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
I need to get categories like beauty personal care, eye ware and their respective urls in get field.How can i loop through this and get the.So far i tried like this and dont no how to proceed next.Can anybody give me suggestions how to parse this json?
json = jParser.getJSONFromUrl(response);
JSONObject api = json.getJSONObject("apiGroups");
JSONObject affiliate = api.getJSONObject("Affiliate");
JSONObject list = affiliate.getJSONObject("listingsAvailable");
You can read the documentation about the JSONObject class in Android.
In this documentation, you will find the method keys that will "Returns an iterator of the String names in this object."
So you just have to call this method and use the iterator.
Iterator<String> keysIterator = jsonObject.keys();
String key;
while (keysIterator.hasNext()) {
key = keysIterator.next();
//use the key to retrieve the data form jsonObject
}
However, if you are the one generating this json, you may consider changing it a bit. The data in the listingsAvailable should probably be in an array.
Related
I need help in converting below mongo query to an ArrayList so that I can execute the query in spring boot app. Having tough time to convert it in java using Aggregate classe's match, group, etc. method.
private List aggregateInspectionWorkOrders(ArrayList operations) {
MongoCollection mongoCollection = db().getCollection(Constants.DB.MyTable, InspectionWorkOrder.class);
return mongoCollection.aggregate(operations).into(new ArrayList<>());
}
db.mytable.aggregate([
{
$match: {
$and: [
{
deleted: { $eq: false }
},
{
status: {
$in: ['O', 'IP']
}
}
]
}
},
{
$group: {
_id: {
sub: '$sub',
pre: '$pre'
},
location: { $addToSet: { mile: '$mp'}}
}
},
{
$group: {
_id: {
sub: '$_id.sub'
},
location: {
$addToSet: {
pre: '$_id.pre',
from: {
$min: '$location.mp'
},
to: {
$max: '$location.mp'
}
}
}
}
},
{
$project: {
_id: 0,
sub: '$_id.sub',
location: '$location'
}
}
]).pretty()
I am working on getting map type data stored in DynamoDB, the way I have tried so far:
use String getData() in my POJO, however, there is an issue that DynamoDBMappingException: could not unconvert attribute.
use Map<String, Object> getData() and annotated with #DynamoDBTypeConverted(converter = MapConverter.class) to retrieve the data, and also wrote a converter for it. This method also throws Exception DynamoDBMappingException: could not unconvert attribute, expected S in value.
So my question is:
is there a way to retrieve the standard JSON format data from DynamoDB by using mapper instead of DynamoJson without “N”, “S”, etc?
For instance:
"data": {
"cId":"777",
"cName":"NPR"
"isOwner":true
}
Once I get the standard JSON, I can use objectmapper to convert it to arbitrary data based on different use cases.
2. is it possible to use a map here? I found some folks create POJO for nested data, however, in my use case, the data fields vary from the different scenarios.
The converter I wrote:
public class MapConverter implements DynamoDBTypeConverter<String, Map> {
private final ObjectMapper objectMapper;
public MapConverter() {
objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
}
#Override
public String convert(final Map map) {
if (map == null) {
return null;
}
try {
return objectMapper.writeValueAsString(map);
} catch (JsonProcessingException e) {
log.error("Hit exception while trying to convert map. {}.", map, e);
return null;
}
}
#Override
public Map unconvert(String serializedMap) {
if (serializedMap == null) {
return null;
}
try {
return objectMapper.readValue(serializedMap, Map.class);
} catch (IOException e) {
log.error("Hit exception while trying to unconvert map. {}.", serializedMap, e);
return null;
}
}
}
The data in the DynamoDB is like this:
{
"data": {
"M": {
"cId": {
"S": "777"
},
"cName": {
"S": "NPR"
},
"isOwner": {
"BOOL": true
}
}
}
}
and it may also look like
"data": {
"M": {
"globalEntityId": {
"S": "234"
},
"providerMetadata": {
"L": [
{
"M": {
"proj.123": {
"M": {
"entityId": {
"S": "s:123"
}
}
},
"PA": {
"M": {
"entityId": {
"S": "p:123"
}
}
}
}
}
]
}
}
}
I am trying to retreive customer object from my rest api. I generated the api using spring data jpa. I have used volley to retrive the information from the api. I can't tell what i did wrong. As i am new to android i don't have much idea. can some one help me to parse the customer object from my Json api.
my api looks like this:
{
"_embedded": {
"customers": [
{
"firstName": "Alexander",
"lastName": "arnold",
"email": "trentarnold#liverpool.com",
"password": "cornertakenquickly",
"_links": {
"self": {
"href": "http://localhost:8080/api/customers/1"
},
"customer": {
"href": "http://localhost:8080/api/customers/1"
}
}
},
{
"firstName": "test",
"lastName": "tester",
"email": "dulalsujan911#gmail.com",
"password": "12345678",
"_links": {
"self": {
"href": "http://localhost:8080/api/customers/2"
},
"customer": {
"href": "http://localhost:8080/api/customers/2"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/api/customers{?page,size,sort}",
"templated": true
},
"profile": {
"href": "http://localhost:8080/api/profile/customers"
}
},
"page": {
"size": 20,
"totalElements": 2,
"totalPages": 1,
"number": 0
}
}
my code looks like this i am using volley :
// connects to the api and stores the retrived JSON values in the respective list
public void connectToApi(){
// initialize lists
emailList = new ArrayList<>();
passwordList = new ArrayList<>();
final String BASE_URL ="http://192.168.1.67:8080/api/customers";
// final String BASE_URL ="http://10.0.2.2:8080/api/customers";
// creating a request ques for HTTP request
RequestQueue requestQueue = Volley.newRequestQueue(this);
// Setting HTTP GET request to retrieve the data from the SERVER
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET,BASE_URL
,null
, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("customers");
for(int i=0; i<jsonArray.length();i++){
JSONObject customer = jsonArray.getJSONObject(i);
emailList.add(customer.getString("email"));
passwordList.add(customer.getString("password"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
} , new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("REST error",error.toString() );
}
}
);
requestQueue.add(objectRequest);
}
Do this :
#Override
public void onResponse(JSONObject response) {
try {
JSONObject json = new JSONObject(response);
JSONObject json_embedded = json.getJSONObject("_embedded");// need to access JSONObject("_embedded")
JSONArray jsonArray = json_embedded.getJSONArray("customers"); // then get JSONARRAY
for(int i=0; i<jsonArray.length();i++){
JSONObject customer = jsonArray.getJSONObject(i);
emailList.add(customer.getString("email"));
passwordList.add(customer.getString("password"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
NOTE : your json array (customers) is in _embedded that's why
it is showing exception.
You need to access first to _embedded object.
try {
JSONObject embedded = response.getJSONObject("_embedded");
JSONArray jsonArray = embedded.getJSONArray("customers");
for(int i=0; i<jsonArray.length();i++){
JSONObject customer = jsonArray.getJSONObject(i);
emailList.add(customer.getString("email"));
passwordList.add(customer.getString("password"));
}
} catch (Exception e) {
e.printStackTrace();
}
I have a special need where in I need to query elastic search for a field say F1.
F1 is not there in all documents . My search should be for missing F1 documents and F1 with some value.
F1 is missing or F1 = 'A1'
I am not sure if this is possible in elastic search. Here is my query and I know that is not right query. Would appreciate somebody could correct it or the Java program.
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"bool": {
"should": {
"terms": {
"F1": [
"Some Value"
]
}
}
}
},
{
"missing": {
"field": "F1"
}
}
]
}
}
}
}
}
This is my java code building the query.
public QueryBuilder getQueryBuilder() {
QueryBuilder qb;
//what do we want to return if there is no request? Nothing or all.
if (filters == null || filters.isEmpty()) {
qb = QueryBuilders.matchAllQuery();
} //build our filtered query using given filters in our request
else {
Set<String> keys = filters.keySet();
BoolFilterBuilder boolFilterBuilder = FilterBuilders.boolFilter();
for (String key : keys) {
Set<Object> values = filters.get(key);
if (values == null || values.toString().isEmpty()) {
continue; //Ignore nothing to do.
}
if (key.equalsIgnoreCase(Constants.MISSING_FILTER)) {
Iterator i = values.iterator();
while (i.hasNext()) {
boolFilterBuilder.must(FilterBuilders.missingFilter((String) i.next()));
}
} else {
boolFilterBuilder.must(buildShouldQuery(key, values));
}
}
qb = QueryBuilders.filteredQuery(createSearchQueryBuilder(), boolFilterBuilder);
}
return qb;
}
Try whit this.
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"should": [
{ "terms": { "F1": ["Some Value"] },
{ "missing": { "field": "F1" } }
]
}
}
}
}
}
I am stuck here and would like to extract using java the second link of the facebook query below
{
"data": [
{
"attachment": {
"media": [
{
"photo": {
"images": [
{
"src": "https://fbcdn-photos-h-a.akamaihd.net/hphotos-ak-prn2/1508634_699393523428883_996610253_s.png"
},
{
"src": "https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-prn2/s720x720/1508634_699393523428883_996610253_n.png"
}
]
}
}
]
}
}
]
}
my code below, is obviously not working
try
{
List<JsonObject> queryResults = facebookClient.executeFqlQuery(query, JsonObject.class);
if(!queryResults.isEmpty())
{
JsonObject facebookPosturl_J = queryResults.get(0);
facebook_post = facebookPosturl_J.getString("src");
}
}
catch (Exception e){logger.warn("Unexpected error", e);}
Try calling:
facebookPosturl_J.getJsonArray("data").getJsonObject(0).getJsonObject("attachment").getJsonArray("media").getJsonObject(0).getJsonObject("photo").getJsonArray("images").getJsonObject(1).getString("src")