need to save all the values coming in for loop? - java

if (Username.equals(RegisteredUSer)) {
JSONArray nameObject = msg.getJSONArray("NameList");
for (int j = 0; j < nameObject.length(); j++) {
JSONObject name = nameObject.getJSONObject(j);
USername = name.getString("FirstName");
USerweight = name.getString("Weight");
Weight = Double.parseDouble(USerweight);
}
}
HERE i need to save all the username and weight coming within for loop and convert the userweight in double ,and extracting the save values within the same class??

Your version:
ArrayList<String> username = new ArrayList<String>();
ArrayList<Double> weight = new ArrayList<Double>();
if (Username.equals(RegisteredUSer)) {
JSONArray nameObject = msg.getJSONArray("NameList");
for (int j = 0; j < nameObject.length(); j++) {
JSONObject name = nameObject.getJSONObject(j);
USername = name.getString("FirstName");
USerweight = name.getString("Weight");
Weight = Double.parseDouble(USerweight);
username.add(USername);
weight.add(Weight);
}
}

U can use a string array or an arraylist.The syntax is ArrayList username=new ArrayList (); username.add (variablename); to extract the values call username.get (loopvariable);

Related

How to parse array into array JSON

first of all I wanted to know if the structure of the json is correct, you can find the JSON here:
http://demo8461125.mockable.io/whitemage
if it is correct I would like to know how to parse "PANINI" , is an array inside an array "tipopietanza"
JSONArray arr = response.getJSONArray("pietanze");
for (int i = 0; i < arr.length(); i++)
{
JSONArray pietanze = response.getJSONArray("PANINI");
List<Pietanze> listapietanze =new ArrayList<>(pietanze.length());
for(int j=0;j<pietanze.length();j++)
{
Pietanze tmp = new Pietanze();
tmp.setNome(pietanze.getJSONObject(j).getString("nomepietanza"));
listapietanze.add(tmp);
}
expandableListDetail.put(arr.getJSONObject(i).getString("tipopietanza"), listapietanze);
}
Yes, it should work, but I suggest just use Gson:
Type type = new TypeToken<List<TipiPaniniAndServizi>>(){}.getType();
List<TipiPaniniAndServizi> tipiPaniniAndServizi = gson.fromJson(json, type);
And save your time from manipulate JSON, just think about your java objects.
JSON structure is okay.
You need to do minor changes in your code for getting list properly.
JSONArray arr = response.getJSONArray("pietanze");
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonObject = arr.getJSONObject(i);
if(jsonObject.has("PANINI")) {
JSONArray paniniJsonArray = jsonObject.getJSONArray("PANINI");
List<Panini> listPanini = new ArrayList<>();
for (int j = 0; j < paniniJsonArray.length(); j++) {
Panini panini = new Panini();
panini.setId(paniniJsonArray.getJSONObject(j).getString("id"));
panini.setNomepietanza(paniniJsonArray.getJSONObject(j).getString("nomepietanza"));
panini.setPrezzo(paniniJsonArray.getJSONObject(j).getString("prezzo"));
listPanini.add(panini);
}
expandableListDetail.put(arr.getJSONObject(i).getString("tipopietanza"), listPanini);
}
}

Fetch value from JSON array based on another value in the array

Here is my JSON response:
[
{
"name":"Canara Atm",
"location_name":"Baker Junction"
},
{
"name":"Canara Atm",
"location_name":"Manarcaud"
},
{
"name":"Canara Atm",
"location_name":"Palai Arunapuram"
},
{
"name":"Canara Atm",
"location_name":"Changanacherry"
}
]
I need to get add values based on a specific location in to to the array, means, for example, location_name":"Baker Junction" is my needed location then to my array I should add only the specified atm name. in the below code all names are being added to the array
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject json = null;
final String xcoords[] = new String[jsonArray.length()];
if (jsonArray.length() != 0) {
for (int i = 0; i < jsonArray.length(); i++) {
json = jsonArray.getJSONObject(i);
xcoords[i] = (json.getString("name"));
}
}
}
this is what i did , you can convert json to list like given below ,
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
json = jsonArray.getJSONObject(i);
Strring s = (json.getString("name"));
list.add(p)
}
then
//you can directly use this value or you can use the index to again fetch the json or for other purpose
int count =0;
int index = 0 ;
for(Sting s : list){
if(s.equals("yourvalue"){
index = count;
}
count++
}
then
if (jsonArray.length() != 0) {
for (int i = 0; i < jsonArray.length(); i++) {
// your index here
json = jsonArray.getJSONObject(index);
xcoords[i] = (json.getString("name"));
}
}

NullPointerException when assigning string from ArrayList to Java Array

I have a problem with my code, because it throws NullPointerException when I try to assign string from arraylist to array.
String[][] data = new String[idList.size()][];
for(int i = 1; i<=idList.size(); i++) {
data[i][0] = idList.get(i);
data[i][1] = nameList.get(i);
data[i][2] = hList.get(i);
data[i][3] = sList.get(i);
data[i][4] = fList.get(i);
data[i][5] = mList.get(i);
data[i][6] = osList.get(i);
data[i][7] = tsList.get(i);
data[i][8] = podList.get(i);
data[i][9] = pacList.get(i);
}
Can someone please tell me how do I fix that?
data[i] is null, since you initialized data to contain idList.size() null references of type String[].
Change
String[][] data = new String[idList.size()][];
to
String[][] data = new String[idList.size()][10];
which will initialize data to contain idList.size() references to a String array of length 10 (String[10]).
I suggest to define the array properly (in both dimensions )
replace this
new String[idList.size()][];
for something like
new String[idList.size()][10]; // you have 10 different lists there...
and iterate in the for loop with zero base...
Example:
String[][] data = new String[idList.size()][10];
for (int i = 0; i < idList.size(); i++) {
data[i][0] = idList.get(i);
data[i][1] = idList.get(i);
data[i][2] = idList.get(i);
data[i][3] = idList.get(i);
}

Android JSON for statement JAVA

The output keeps repeating the name for each property. Only need to see the name once, and then prop_name and address can print out for each occassion under the single name. The JSON looks like:
[{"pmid":"2","name":"CARLYLE MANAGEMENT","result":"1","properties":[{"prop_id":"32","prop_name":"Bonneville Tower","address":"25801 Lakeshore","city":"Euclid","state":"OH","zip":"44132","lat":"41.6223","long":"-81.5034"}]},
{"pmid":"1","name":"COMMERCIAL ONE REALTY","result":"18","properties":[{"prop_id":"3","prop_name":"Autumn Chase Apartments","address":"146 Grand Circuit Blvd.","city":"Delaware","state":"OH","zip":"43015","lat":"40.3105","long":"-83.1138"},
{"prop_id":"6","prop_name":"Barrington Club Apartments","address":"4600 Barrington Club","city":"Columbus","state":"OH","zip":"43220","lat":"40.0536","long":"-83.0448"},{"prop_id":"17","prop_name":"Battleship Building Condominium","address":"444 North Front St.","city":"Columbus","state":"OH","zip":"43215","lat":"39.9712","long":"-83.0042"}]
The code is currently:
String pmid = mJsonObject.getString("pmid");
String name = mJsonObject.getString("name");
String result = mJsonObject.getString("result");
JSONArray mJsonArrayProperty = mJsonObject.getJSONArray("properties");
int innerLength = mJsonArrayProperty.length();
for (int k = 0; k < innerLength; k++) {
// JSONArray mJsonArrayProperty = mJsonObject.getJSONArray("properties");
// for (int i = 0; i < mJsonArrayProperty.length(); i++) {
JSONObject mJsonObjectProperty = mJsonArrayProperty.getJSONObject(k);
String prop_id = mJsonObjectProperty.getString("prop_id");
String prop_name = mJsonObjectProperty.getString("prop_name");
String address = mJsonObjectProperty.getString("address");
String city = mJsonObjectProperty.getString("city");
String state = mJsonObjectProperty.getString("state");
String zip = mJsonObjectProperty.getString("zip");
String lat = mJsonObjectProperty.getString("lat");
String lon = mJsonObjectProperty.getString("long");
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_EMAIL, prop_name);
contact.put(TAG_NAME, name);
contact.put(TAG_PHONE, address);
// adding contact to contact list
contactList.add(contact);
}
} }catch (JSONException e) {
e.printStackTrace();
}
The output gives me the prop_name and address but it also says the name over and over each time. The goal is to see the name once and then the multiple properties under it. How can this be accomplished with a for or if statement, or any other way.
I am just writing the algo, How you can do it...
First you have to get the JSONObject in which you have the jsondata.
From that JSONObject you should get JSONArray because the whole jsondata you have is an array.
Now from that JSONArray you should get the string pmid, name etc..
Now again you have to get JSONArray because properties is JSONArray now you can iterate it.
And for doing this you should have use two for loop or any iteration method you want to use.
Try Out this...
JSONArray one=new JSONArray(json);
for (int i = 0; i < one.length(); i++)
{
JSONObject jsonObject=one.getJSONObject(i);
String pmid=jsonObject.getString("pmid");
String name=jsonObject.getString("name");
String result=jsonObject.getString("result");
JSONArray properties=jsonObject.getJSONArray("properties");
for (int j = 0; j < properties.length(); j++)
{
JSONObject js=properties.getJSONObject(j);
String prop_id=js.getString("prop_id");
String prop_name=js.getString("prop_name");
String address=js.getString("address");
String city=js.getString("city");
String state=js.getString("state");
String zip=js.getString("zip");
String lat=js.getString("lat");
String Long=js.getString("long");
}
}

new to json parsing - how to get an array within an array

from the following link , I need to get the value stored in "media".
Here is my code:
Content = Content.replace("jsonFlickrFeed(", "");
Content = Content.replace("})", "}");
jsonResponse = new JSONObject(Content);
JSONArray jsonMainNode = jsonResponse.optJSONArray("items"); // this works great!
But I can not access past "items"
You will have to loop through the JSON like this:
...
JSONArray jsonMainNode = jsonResponse.optJSONArray("items");
for (int i=0; i<jsonMainNode.length(); i++) {
String media = jsonMainNode.getJSONObject(i).getString("media");
}
This will loop through the images and return the value(s) in media.
In your case it should be something like this:
..
JSONArray jsonMainNode = jsonResponse.optJSONArray("items");
for (int i=0; i<jsonMainNode.length(); i++) {
JSONObject finalNode = jsonMainNode.getJSONObject(i);
JSONArray finalArray = finalNode.optJSONArray("media");
for (int j=0; j<finalArray.length(); j++) {
String m = finalArray.getJSONObject(j).getString("m");
}
}
...because there is another node inside the media node, called m.
Here is an example getting the tags string for each item in the JSONArray that you have:
for (int i = 0; i < jsonMainNode.length(); i++){
JSONObject jsonObject = jsonMainNode.getJSONObject(i);
String tags = jsonObject.getString("tags");
}
This will iterate through all JSONObjects that are in the array, and extract the tags field from each object.
JSONArray jsonMainNode = jsonResponse.optJSONArray("items"); // this works great!
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject item = (JSONObject) jsonMainNode.get(i);
JSONObject media = item.getJSONObject("media");
String valueMedia = media.getString("m");
Log.d("TAG", valueMedia);
}

Categories

Resources