Detecting weather using JSON parsing - android

How to parse code, messge, calctime,city,id,country, name from following
use this URL : http://openweathermap.org/data/2.1/forecast/city/524901
{ "cod":"200","message":"kf","calctime":0.0342,"url":"http:\/\/openweathermap.org\/city\/524901",
"city":
{
"id":524901,
"coord":
{
"lon":37.615555,"lat":55.75222
},
"country":"RU","name":"Moscow","dt_calc":1356948005,"stations_count":6
},

Follow the below code:
JSONObject jObj=new JSONObject(jsonResponse);
String msg=jObj.getString("message");
String calctime=jObj.getString("calctime");

Use below code for parse code, messge, calctime,city,id,country, name from above url, it will solve your problem.
JSONObject mJsonObj = new JSONObject(mJsonResponse);
String mCode = mJsonObj.getString("cod");
String mMessage = mJsonObj.getString("message");
String mCalcTime = mJsonObj.getString("calctime");
JSONObject mJsonCityObj = mJsonObj.getJSONObject("city");
String mId = mJsonCityObj.getString("id");
String mConuntry = mJsonCityObj.getString("country");
String mName = mJsonCityObj.getString("name");

Related

Parsing Single JSON object in Android Studio

I have a JSON Object I want to parse at this URL https://api.adviceslip.com/advice with this content:
{"slip": { "id": 137, "advice": "You're not that important; it's what you do that counts."}}
I have written this code in Android Studio but it does not seem to work.
String jsonString = handler.httpServiceCall(url);
if (jsonString != null) {
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject slip = jsonObject.getJSONObject("slip");
String id = slip.getString("id");
Log.d("slip id:", id);
String advice = slip.getString("advice");
Log.d("slip adv:", advice);
HashMap<String, String> map = new HashMap<>();
map.put("id", id);
map.put("advice", advice);
adviceSlip.setText(map.get("advice"));
}
Any help would be appreciated :)
One problem is that you are trying to get a String on an Int object, and perhaps this is one of your errors, please change your
splip.getString("id");
by
slip.getInt("id);
Another one is that you are creating a HashMap<String,String> but the id you getting from the json is an Int and perhaps you should change it to use HashMap<Int,String>
You can get the value from the jsonobject and simple set that value on the textview.
String jsonString = handler.httpServiceCall(url);
if (jsonString != null) {
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject slip = jsonObject.getJSONObject("slip");
String id = slip.getString("id");
Log.d("slip id:", id);
String advice = slip.getString("advice");
adviceSlip.setText(advice);
}

How to get data from json objects

How to get the particular data object from the JSON data from the given JSON data:
{
"customer":{
"id":1117198024800,
"email":"abc#gmail.com",
"accepts_marketing":false
}
}
I need to parse ID from the above data, Can anyone help me please. Thanks in advance!!!
If you will use it as string:
JSONObject reader = new JSONObject(data);
JSONObject customer = reader.getJSONObject("customer");
String id = (String) customer.getString("id");
Use this
JSONObject responceObj = new JSONObject(data);
JSONObject customer= response.getJSONObject("customer");
String id= customer.getString("id");
String email= customer.getString("email");
String accepts_marketing= customer.getString("accepts_marketing");
add to app/build.gradle:
dependencies {
...
implementation "com.google.code.gson:gson:2.8.1"
}
In code:
String string = "{
\"customer\":{
\"id\":1117198024800,
\"email\":\"abc#gmail.com\",
\"accepts_marketing\":false
}
}";
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<HashMap<String,Customer>>() {}.getType();
HashMap<String, Customer> hashMap = new Gson().fromJson(string, type).
Customer customer = hashMap.get("customer");
Customer.java class:
public class Customer{
Long id;
String email;
Boolean acceptsMarketing;
}

Filter array by index and save it to another array

I´m trying to filter all values in name where id = 1 and save to an array.
This is the output I get from my WS:
[{id=1; name=Des Moines; }, {id=2; name=Cedar Rapids; }, {id=3; name=Yakima; },{id=4; name=Fort Dodge; }, {id=1; name=Iowa City; }]
if I try get property it splits my content.
Could anyone help me out on how to get the desire output?
That's JSON data, you should consider parse the data into an object , there are a lot of JSON java parsers , you could read something about GSON:
https://github.com/google/gson/blob/master/UserGuide.md
You can try something like the following
your json string must be like:
["items":{id=1; name=Des Moines; }, {id=2; name=Cedar Rapids; }, {id=3; name=Yakima; },{id=4; name=Fort Dodge; }, {id=1; name=Iowa City; }]
JSONObject jsonObject = null;
jsonObject = new JSONObject("your json string goes here!!");
String items = jsonObject.getString("items");
JSONArray array = new JSONArray(weather);
ArrayList<JSONObject > arrList = new ArrayList<JSONObject >();
for(int i = 0; i < array.length(); i++)
{
JSONObject jsonObject1 = array.getJSONObject(i);
arrList.add(jsonObject1 );
String id = jsonObject1.get("id").toString();
String name = jsonObject1.get("name").toString();
}

Accessing Json object information

I am developing an android app, in that i have to make a call to my remote server,it will give me data in terms of json object. in following format.
{
-source: {
LS: " ABCDEF",
name: "XYXA",
point: "77.583859,12.928751"
},
-stores: [
-{
ph: null,
distance: "0.3",
LS: " abcd",
id: 1209,
name: "xyz",
point: "77.583835,12.926359"
},
-{
ph: null,
distance: "0.3",
LS: " abcd",
id: 1209,
name: "xyz",
point: "77.583835,12.926359"
}
]
}
i have confirmed that server is giving me response. But i'm not getting how to access this data in my application.
Can anybody give me a code to access these data.?
Thanking you
JSONObject jsonObject=new JSONObject(responseString);
String LS=jsonObject.getJSONObject("source").get("LS").toString();
String name=jsonObject.getJSONObject("source").get("name").toString();
String point=jsonObject.getJSONObject("source").get("point").toString();
String[] latlng = point.split(",");
String lat=latlng[0];
String lng=latlng[1];
System.out.println("Lat "+lat+" Lng "+lng);
JSONArray jsonArray=jsonObject.getJSONArray("stores");
if (jsonArray.length()>0)
{
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject jsonObject1=jsonArray.getJSONObject(i);
String ph=jsonObject1.get("ph").toString();
String distance=jsonObject1.get("distance").toString();
String LS=jsonObject1.get("LS").toString();
String id=jsonObject1.get("id").toString();
String name=jsonObject1.get("name").toString();
String point=jsonObject1.get("point").toString();
String[] latlng = point.split(",");
String lat=latlng[0];
String lng=latlng[1];
System.out.println("Lat "+lat+" Lng "+lng);
}
}
Here is the complete parsing code section.
Try to parse your JSON response as below:
JSONArray arra = new JSONArray("stores");
for(int i=0;i<arra.length();i++)
{
JSONObject json = arra.getJSONObject(i);
//Retrieve the data from the JSON object
String name= json.getString("name");
String points = json.getString("point");
String loc=json.getString("LS");
}
EDITED:
Extract the lat,long from string point you need to use the StringTokenizer to split the string as below:
StringTokenizer stoken = new StringTokenizer(points, ",");
while (stoken.hasMoreTokens()) {
System.err.println(stoken.nextToken());
}

How to get value from one JSONObject to another JSONObject

i want to retrive data from one JSONObject in Andother JSONObject
Can any one tell me how to retrive them
My JSONObject is like this
{"udetail":{"ID":238597,"Reference":"AGT-ALIWF_TEST","Provider":"TAL","DropDate":"2012-12-29T13:06:00","abc":"South","def":"2013-01-06T13:06:00","ghi":"North"},"jkl":{"Title":"Mr","FirstName":"LastName_TEST","LastName":"FirstName_TEST LastName_TEST"},"mydetail":{"my":"Model_TESTMake_TEST","hi":"Colour_TEST","tget":"A123 XYZ"}}
String s= "{
"OrderDetails": {
"ID": 238597,
"Reference": "AGT- ALIWF_TEST",
"Provider": "TAL",
"DropDate": "2012-12- 29T13:06:00",
"DropTerminal": "South",
"ReturnDate": "2013-01- 06T13:06:00",
"ReturnTerminal": "North"
},
"CustomerDetails": {
"Title": "Mr",
"FirstName": "LastName_TEST",
"LastName": "FirstName_TEST LastName_TEST"
},
"CarDetails": {
"Make": "Model_TESTMake_TEST",
"Colour": "Colour_TEST",
"Registration": "A123 XYZ"
}
}";
Try this
JSONObject obj=new JSONObject(s);
String orderDetails=obj.getString("OrderDetails");
JSONObject orderjson=new JSONObject(orderDetails);
String Reference=orderjson.getString("Reference");
Same for CarDetails and customer Details
Try this,
To get order details,
JsonObject jobj = new JsonObject(Response);
String str = jobj.getJSONObject("OrderDetails").getString("ID");
customer details,like that
String str1 = jobj.getJSONObject("CustomerDetails").getString("Title");
car details,
String str2 = jobj.getJSONObject("CarDetails").getString("Make");
You should specify in more detail what you want to achieve and what you have tried. If what you've posted is actually a JSONObject and not a String containing some JSON, you should be able to simply fetch e.g. the OrderDetails object using
JSONObject details = <Your JSONObject>.getJSONObject("OrderDetails");
If what you have is just a string with some JSON, create a JSONObject of it first:
JSONObject jo = new JSONObject("{"OrderDetails":{"ID":238 ... }}
JSONObject details = <Your JSONObject>.getJSONObject("OrderDetails");
Also see:
http://developer.android.com/reference/org/json/JSONObject.html#getJSONObject(java.lang.String)

Categories

Resources