Any suggestions to deserialize this json with Gson? [closed] - android

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Any suggestions to deserialize this json with Gson ?
{
"0": {
"id": 1,
"name": "pepe"
},
"1": {
"id": 2,
"name": "pipo"
},
"2": {
"id": 3,
"name": "pato"
}
...
n objects
}

Convert the Json string to Hashmap like below using Google's Gson, then you can interate through the hashmap
String jsonString = "Your JSON string";
HashMap<String, JsonObject> map = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, JsonObject>>(){}.getType());

Related

I need list of US states and counties of the state [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I need to include the list of US states and state counties locally in the application am working on. So can someone please let me know where can I find that list to download either in Json or .plist format.
Thanks
You can do whatever you want if you put your mind to it.
I've written you a tool to extract this data from a known updated source (Wikipedia).
The code can be downloaded from Github.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using HtmlAgilityPack;
using Newtonsoft.Json;
namespace GetCitiesCounties
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(#"Hitting Wikipedia");
var uri = new Uri("https://en.wikipedia.org/wiki/List_of_United_States_counties_and_county_equivalents");
var client = new HttpClient();
var rs = client.GetAsync(uri).Result;
if (rs.IsSuccessStatusCode)
{
var htmlContent = rs.Content.ReadAsStringAsync().Result;
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(htmlContent);
var list = new List<dynamic>();
var nodes = htmlDoc.DocumentNode.SelectNodes("//table[#class='wikitable sortable']//tr");
Console.WriteLine(#"Processing Rows");
int rowIndex = 0;
foreach (var row in nodes)
{
if (rowIndex++ > 0)
{
var county = row.SelectNodes("td")[1].InnerText;
var state = row.SelectNodes("td")[2].InnerText;
list.Add(new
{
County = county,
State = state
});
}
}
var json = JsonConvert.SerializeObject(list);
File.WriteAllText(#"C:\test.json", json);
Console.WriteLine(#"Done, extracted cities and states to json file C:\test.json");
Console.ReadLine();
}
}
}
}
This is a snippet of the result. It shows each county in the US and the state it's attached to.
[
{
"County": "Autauga County",
"State": "Alabama"
},
{
"County": "Baldwin County",
"State": "Alabama"
},
{
"County": "Barbour County",
"State": "Alabama"
}
]
You can find list here : https://www.webtoolz.online/resources/usa-list-of-states
This list contains state names and abbreviations in JSON format.

How to read a JSON value? [duplicate]

This question already has answers here:
Parsing JSON Object in Java [duplicate]
(5 answers)
Closed 5 years ago.
this is my json response
{
"status": true,
"code": 200,
"message": "Success",
"data": {
"id": "14"
}
}
how to read this values
You can use the help of Google's GSON library built for exact your pupose.
YourPojoClass model = new Gson().fromJson(jsonResponse, YourPojoClass.class);
Convert your json to PojoModel by using jsonschema2pojo.org
You can do it like this:
Create a JSONObject:
JSONObject jObject = new JSONObject(result);
To get a specific boolean
String aJsonString = jObject.getBoolean("status");
To get a specific String
String aJsonString = jObject.getString("message");
To get a specific Integer
int aJsonInteger = jObject.getInt("code");

How to filtering data for two values in FirebaseListAdapter? [duplicate]

This question already has answers here:
Query based on multiple where clauses in Firebase
(8 answers)
Closed 5 years ago.
I am use Firebase DB and FirebaseListAdapter for display and filtering my data. But, my data is filtering for ONE values (idUser). But, i want filtering data for TWO values idUser and good (show data where idUser=2 and good=true).
Can i try it in android?
firebaseListAdapter = new FirebaseListAdapter<Movie>(this, Movie.class,
R.layout.item_movie, FirebaseDatabase.getInstance().getReference()
.child("movies")
.orderByChild("idUser")
.equalTo("2"))
My database:
{
"movies": {
"1": {
"name": "comedy",
"idUser": "2",
"good": "true"
},
"2": {
"name": "Horror",
"idUser": "2",
"good": "false"
},
"3": {
"name": "comedy",
"idUser": "2",
"good": "true"
}
}
}
You can only use order by once.
So the answer is to modify your schema.
See my answer to this question multiple properties

How to get string from multiple json array inside a json object [duplicate]

This question already has answers here:
Sending and Parsing JSON Objects in Android [closed]
(11 answers)
Closed 7 years ago.
{
"data:"
{
"calendar":
[
{
"Booked":["13-11-2015","12-12-2015"],
"Available":["20-12-2015","22-12-2015"]
}
]
}
}
Could any one please help me how to get date from json array string
Your json should be like this
{
"data": {
"calendar": [
{
"Booked": [
"13-11-2015",
"12-12-2015"
],
"Available": [
"20-12-2015",
"22-12-2015"
]
}
]
}
}
You can use json online parser to check the validity of a json.
Now, coming back to your actual question.I dont have an android IDE so I will post a quick code to give u an idea
JSONObject jparentobj= new JSONObect("jsonString");
JSONObject datajObj= jparentobj.getJSONObect("data");
JSONArray calenderarray = datajObj.getJSONArray{"calender");
for (int i = 0; i < calenderarray.size(0); i++){
String booked = calenderarray.get(i).getString("Booked");
//Like this get each entity
}
Hope this helps you..

How to show json array object in textview [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to add json array in textview? I want to show json array object "name" in text view. How do I do it? How to getting single element inside array? I want to print json array obect name in textview
JSONObject json2 = new JSONObject(str);
status = json2.getString("status");
if (status.equals("1")) {
JSONArray school2 = json2.getJSONArray("data");
txt1.setText(data2.getString("name"));
}
"status":1,
"data":
[
{
"dish_id":"37",
"name":"dish2",
"description":"testing",
"allergen":
"Eggs3,walnuts",
"image":"http:\/\/198.57.208.46\/~school\/uploads\/images\/dishes\/egg-biryani.jpg",
"ingredient":"weqweqw23,dsfds"
}
],
"dish_nutrition":
{"1":
{
"name":"Cholesterol and Diet",
"qty":"2"
},"2":
{
"name":"Cholesterol and Diet",
"qty":"1"
}
}
}
You can access the field like this:
school2.getJSONObject(0).getString("name")
For the details look it up in the documentation

Categories

Resources