i want to get avatar_url in actor object. i can get "id" and "type" to show in recyclerview. but avatar_url not show image why?
json url
final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
for(int i=0; i<response.length(); i++) {
JSONObject jsonObject = response.getJSONObject(i);
Article article = new Article();
article.setAvatar_url(jsonObject.getString("avatar_url"));
article.setId(jsonObject.getString("id"));
article.setType(jsonObject.getString("type"));
articles.add(article);
how i get avatar_url from actor field?
and yes i can run because i delete
article.setAvatar_url(jsonObject.getString("avatar_url"));
Thanks
Looking at your JSON, you can see that the "avatar_url" isn't a peer of the "id" and "type".
"id": "11392115556",
"type": "PushEvent",
"actor": {
"id": 8517910,
"login": "LombiqBot",
"display_login": "LombiqBot",
"gravatar_id": "",
"url": "https://api.github.com/users/LombiqBot",
"avatar_url": "https://avatars.githubusercontent.com/u/8517910?"
},
You need to retrieve the "actor" object and then get the "avatar_url" from within that object.
Related
There is data in the form of json.
{
"type": "1C",
"counterparty": {
"name": "ИП Петя",
"inn": "123456789",
"kpp": ""
},
"nomenclatures": [
{
"cod": [{"type":"1C", "val":"РТ-1234"}, {"type":"Front", "val":"567"}],
"name": "Картоха",
"article": "123"
},
{
"cod": [{"type":"1C", "val":"РТ-1234"}, {"type":"Front", "val":"567"}],
"name": "Сникерс",
"article": "459"
}
]
}
There is a database.
enter image description here
3.Now I want to save this entity to database.
I saved three entities. There is one entity left that I cannot save. Help save the fourth entity Cod to the database.
#PostMapping(value = "/item")
public void postMain (#RequestBody NodeObject node) {
Counterparty counterparty = new Counterparty();
counterparty.setName(node.getCounterparty().getName());
counterparty.setInn(node.getCounterparty().getInn());
counterparty.setKpp(node.getCounterparty().getKpp());
counterpartyRepository.save(counterparty);
NodeObject nodeObject = new NodeObject();
nodeObject.setType(node.getType());
nodeObject.setId(counterparty.getId());
nodeObject.setCounterparty(counterparty);
nodeObject.setNomenclatures(node.getNomenclatures());
nodeObjectRepository.save(nodeObject);
Set<Nomenclature> nomenclatures = node.getNomenclatures();
nomenclatures.forEach(nomenclature -> nomenclature.setNodeObject(nodeObject));
nomenclatureRepository.saveAll(nomenclatures);
// ????????????????????????
List<Cod> cod = new ArrayList<>();
}
json comes to the controller. #RequestBody NodeObject node
I have learned how to use a rest template to access a JSON array like this:
[
{
"ID": "0ae6496f-bb0b-4ebd-a094-ca766e82f3e7",
"Confirmed": 0,
}
{
"ID": "e010ced5-c7cb-4090-a7ed-206f4c482a5b",
"Confirmed": 0,
}
]
I accessed the Confirmed for example with
public Model[] getModel() {
ResponseEntity<Model[]> response = restTemplate.getForEntity(apiUrl, Model[].class);
return response.getBody();
}
but now I have to access data in another Json from another API. The data looks like this
{
"prefixes": [
{
"region": "ap-northeast-2",
"service": "AMAZON",
},
{
"region": "eu-west-3",
"service": "AMAZON",
}
]
}
How can I access region or service inside, and what would be the proper name for this?
The first is a JSON array, the second a JSON object?
The first API is simply
https://example.com
Whereas the second is
https://example.com/data.json.
You have to create the POJO for return type:
List<RegionServiceObject> items;
Where RegionServiceObject looks:
public class RegionServiceObject {
private String region;
private String service;
// constructors, getters/setters, toString()....
}
The way of deseariliseing to object is similar to which you already wrote:
RegionServiceObject[] items = restTemplate.getForObject(url, RegionServiceObject[].class);
and access for specific item will be as usual for specific item:
for (RegionServiceObject item : items) {
item.getRegion();
item.getService();
// use them here
}
Can you tell me how to create a Json objecct for this
I think it is a JSON with children JSON objects.
[{
"data": {
"ID": "56e819a90111257894be41c9e310f8cb",
"name": "Email_____2016-03-15 10_18_16",
"objCode": "DOCU",
"description": null,
"lastUpdateDate": "2016-03-15T10:18:17:551-0400"
}
}: null]
I did this
JSONObject childVal = new JSONObject();
JSONObject parent = new JSONObject();
try {
childVal.put("ID", "56e819a90111257894be41c9e310f8cb");
childVal.put("name", "Email_____2016-03-15 10_18_16");
childVal.put("objCode", "DOCU");
childVal.put("description", "null");
childVal.put("lastUpdateDate", "2016-03-15T10:18:17:551-0400");
parent.put("data", childVal);
but how do i get the parent [ object : null]
thanks
I'd like to deserialize this using GSON into a list of Post, but can't figure out how to tell GSON how to ignore the root element "posts" (as its an object) and just process the array.
I've got:
Type postTypeList = new TypeToken<List<Post>>(){}.getType();
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(myJSONString);
JsonObject postsRootObj = jsonElement.getAsJsonObject();
List<Post> postList = gson.fromJson(postsRootObj.get("posts"), postTypeList);
BUT.. I'd rather not have the whole JsonParser, I'd rather just pass it directly into the gson.fromJson function.. Is there a way to do this?
{ "posts":
[
{
"username": "John",
"message": "I'm back",
"time": "2010-5-6 7:00:34"
"validator":[{
"prog": "Name1",
"name": "Name2",
"computername": "Name3",
"date": "2010-11-20 19:39:55"
}]
}
,
{
"username": "Smith",
"message": "I've been waiting",
"time": "2010-4-6 10:30:26"
"validator":[{
"prog": "Name1",
"name": "Name2",
"computername": "Name3",
"date": "2010-11-20 19:39:55"
}]
}
]}
Create a wrapper class which will have List<Post> as its member
public class PostList{
private List<Post> posts;
// getter and setters for posts
}
And then use fromJson in the similar fashion
List<Post> postList = gson.fromJson(myJSONString,PostList.class);
There is a correction from above. Usage should be :
PostList postList = gson.fromJson(myJSONString,PostList.class);
Post post = postlist.get(index) //index is the index of list you want to access
The response of my service ALFRESCO REST is:
[
{
"role": "SiteManager",
"authority":
{
"authorityType": "USER",
"fullName": "admin",
"userName": "admin",
"firstName": "Administrator",
"lastName": "",
"url": "\/alfresco\/service\/api\/people\/admin"
},
"url": "\/alfresco\/service\/api\/sites\/test3\/memberships\/admin"
}
,
{
"role": "SiteConsumer",
"authority":
{
"authorityType": "GROUP",
"shortName": "jamalgg",
"fullName": "GROUP_jamalgg",
"displayName": "jamalgg",
"url": "\/alfresco\/service\/api\/groups\/jamalgg"
},
"url": "\/alfresco\/service\/api\/sites\/test3\/memberships\/GROUP_jamalgg"
}
,
{
"role": "SiteManager",
"authority":
{
"authorityType": "GROUP",
"shortName": "ALFRESCO_ADMINISTRATORS",
"fullName": "GROUP_ALFRESCO_ADMINISTRATORS",
"displayName": "ALFRESCO_ADMINISTRATORS",
"url": "\/alfresco\/service\/api\/groups\/ALFRESCO_ADMINISTRATORS"
},
"url": "\/alfresco\/service\/api\/sites\/test3\/memberships\/GROUP_ALFRESCO_ADMINISTRATORS"
}
]
And I want to parse to list of object:
List<Memberships > listMemberships;
public class Memberships {
private String role;
private List<Authority> listAuthority ;
private String url;
}
public class Authority {
private String authorityType;
private String shortName;
private String fullName;
private String displayName;
private String url;
}
I think that there are two solutions:
how to add the tag Memberships to JSON result for encapsulates
the whole.
how to parse JSON result directly to my list
Thanks
As answered in a-better-java-json-library I would use the google-gson library.
Thank you Ozoli. The answer to my question is:
Type targetType = new TypeToken<Collection<Memberships>>() {}.getType();
List<Memberships> list = (List<Memberships>) new Gson().fromJson(renduJson,targetType);
You can also use http://jsongen.byingtondesign.com/ to generate java code from json response and then use jackson library ( http://jackson.codehaus.org/ ) to bind that response data to your object(s):
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(new File("c:\\user.json"), User.class);
sorry for not formatting code
Type targetType = new TypeToken<Collection<Memberships>>() {}.getType();
List<Memberships> list = (List<Memberships>)new Gson().fromJson(rendu,targetType);