I got a json response as
{
"tasks": null,
"projects": null
}
how to check if the array has null values and handle the case??
Thanks:)
Try this,
jsonObject.isNull("field-name")
Reference: JSON null handling and this.
Try This
=============
try {
String url;
JSONObject JsonData = new JSONObject("YOUR FULL JSONSTRING");
JSONArray webData = JsonData.getJSONArray("YOUR ROOT NAME OF YOUR JSON RESPONSE");
for(int i=0;i<webData.length();i++)
{
JSONObject TeampObj = webData.getJSONObject(i);
if(TeampObj.getString("tasks").trim().length()>0)
{
//store data in arrylist or string array
}
if(TeampObj.getString("projects").trim().length()>0)
{
//store data in arrylist or string array
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Try..
public boolean isNull (String name)
Added in API level 1
Returns true if this object has no mapping for name or if it has a mapping whose value is NULL.
use try ... catch to handle the exception:
Try{
// parsing data
}catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Related
I have such a JSON: https://paste.ubuntu.com/p/HkTK9xTzNx/
How do I get a "imdb_id" String (line 14). There is no array which contains that value,it's not in square branches,so I don't know how to get it.
Solution as per your need:
try {
imdbLink = response.getString("imdb_id");
} catch (JSONException e) {
e.printStackTrace();
}
A standard solution:
String json = "{\"imdb_id\":\"str12345\"}"; // your json response from network call/local file
JSONObject jsonObject;
try {
jsonObject = new JSONObject(json);
String id = jsonObject.getString("imdb_id");
} catch (Exception e) {
e.printStackTrace();
}
I have the following method for parsing a sample String to JSONObject:
private JSONObject test() {
try {
String responseData = "{\"m_tani\":[{\"tani_cd\":\"02\",\"tani_nm\":\"cs\"},{\"tani_cd\":\"03\",\"tani_nm\":\"pc\"}]}";
Log.i("Json", responseData.toString());
JSONObject json = new JSONObject(responseData);
return json;
} catch (Exception e) {
e.printStackTrace();
Log.i("Json", "exception");
}
Log.i("Json", null);
return null;
}
The responseData is:
{"m_tani":[{"tani_cd":"02","tani_nm":"cs"},{"tani_cd":"03","tani_nm":"pc"}]}
When I debug it, from the line JSONObject json = new JSONObject(responseData); it jumps to return null;, not return json; or catch(Exception e).
I don't know why, please help me with this
This occurs during step-by-step debugging, when you have multiple return points from a method.
When converting the java bytecode to dalvik, the return calls are merged (for optimization reasons?), and when you debug the code, it may seem that you reach for the wrong one, or call multiple ones. That's not happening though, your code is correct, it's just how it appears when debugging.
You can see this post for more reference
try this code
private JSONObject test() {
try {
String responseData = "{\"m_tani\":[{\"tani_cd\":\"02\",\"tani_nm\":\"cs\"},{\"tani_cd\":\"03\",\"tani_nm\":\"pc\"}]}";
Log.i("Json", responseData.toString());
JSONObject json = new JSONObject(responseData);
return json;
} catch (JSONException e) {
e.printStackTrace();
Log.i("Json", "exception");
return null;
}
}
When i try to run this code in android i am getting the resultant String as "Name":"Text1\/Text2" But the result should be {"Name":"Text1/Text2"}.
try {
String str;
JSONObject json = new JSONObject();
json.put("Name", "Text1/Text2");
str = json.toString();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thanks.
As #GrlHu said that by default the android will convert your string into utf-8 encoding format so your / will be replaced with \/. Please read the following two post 1. JSON: why are forward slashes escaped?
2. Why is the slash an escapable character in JSON?
So instead of this you can use getString(Name) method. Hope you will get the perfect value.
try {
String str;
JSONObject json = new JSONObject();
json.put("Name", "Text1/Text2");
str = json.getString("Name");
Log.e("test", str);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am looking to pull data from my websites JSON url and display only one object in the textview. I was able to parse the entire JSON array, but not the specific object.
Here's the JSON on the site:
{"id":3,"day":" an A","created_at":"2013-11-06T12:30:59.023Z","updated_at":"2013-11-06T12:30:59.023Z"}
As you can see, it's pretty simple, but basically all I want to pull is the
"day":" an A"
and display it in my textview as "an A". Until now, I've only been able to pull the entire array.
A reference to this or any solution would be greatly appreciated!
Thanks in advance!
MainActivity Class:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parseDay);
TextView textView1 = null;
try {
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
JSONObject json=new JSONObject("day");
try {
String day =json.getString("day");
textView1.setText(day);
} catch (JSONException e) {
e.printStackTrace();
}
}
//catch (JSONException e) {
// e.printStackTrace();
//}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and My GetMethod:
public class GetMethod {
public String getInternetData() throws Exception {
BufferedReader in = null;
String data = null;
try {
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://www.xelatechnologies.com/hfdays/show.json");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) !=null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
}
finally {
if (in !=null){
try{
in.close();
return data;
}catch (Exception e){
e.printStackTrace();
}
}
}
}
I'm extremely new to JSON parsing so I'm sure it is not right at all. But it's worth a try!
The JSON you posted is a JSONObject. In java you can put that object into an JSONObject like this (You can use any serialize/deserializer you would like, many exist, for this example try org.json):
String json = "{\"id\":3,\"day\":\" an A\",\"created_at\":\"2013-11-06T12:30:59.023Z\",\"updated_at\":\"2013-11-06T12:30:59.023Z\"}";
JSONObject jsonObject = new JSONObject(json);
Now you have created a json object. Next you want to set the text on the text view. The key to get your value in this case is "day". So now all you have to do is use the provided getString(String value) method on the json object.
final String DAY = "day";
String dayValue= "";
try {
value = jsonObject.getString(DAY);
} catch (JSONException e) {
e.printStackTrace();
}
TextView textView = findViewById(R.id.text_view);
textView.setText(dayValue);
{"id":3,"day":" an A","created_at":"2013-11-06T12:30:59.023Z","updated_at":"2013-11-06T12:30:59.023Z"}
As its starts from '{' so it is an Object not Array
JSONObject json=new JSONObject("YOUR_JSON_STRING");
try {
String days=json.getString("day");
textview.setText(days);
} catch (JSONException e) {
e.printStackTrace();
}
Edited:
TextView textView1;
String response;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parseDay);
textView1 = (TextView)findViewById(R.id.your_textview_id);//First Initialise TextView
GetMethod method=new GetMethod();
response=method. getInternetData();// Then get the Json response
try{
JSONObject json=new JSONObject(response);
try {
String day =json.getString("day");
textView1.setText(day);
} catch (JSONException e) {
e.printStackTrace();
}
}
catch (JSONException e) {
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And dont forget to add Internet Permission in Manifest file
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Try like this.
I have an ASP.Net 3.5 Webservice (asmx) that returns what appears to be valid JSON. I have validated the returned JSON using an online validator (JSONLint . com) and it says it is valid. I can not figure out how to parse this string.
{
"d": "{\"returnType\":\"authToken\",\"returnData\":\"b1ec28b8-3fca-427a-bbce-8802fb95d94b\"}"
}
Below is my code.
public static JSONObject DotNetJSONResponse(String raw) throws Exception {
JSONObject joRaw;
try {
joRaw = new JSONObject(raw);
JSONObject joD = joRaw.getJSONObject("d");
return joD;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
try this way
public static JSONObject DotNetJSONResponse(String raw) throws Exception {
JSONObject joRaw;
try {
joRaw = new JSONObject(raw);
String str1 = joRaw.getString("d");
JSONObject joD = new JSONObject(str1);
return joD;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
try this one. In your sample response, d is a an attribute, not a JSONObject. So have to parse the string first, then convert the d string as a JSONObject.
public static JSONObject DotNetJSONResponse(String raw) throws Exception {
JSONObject joRaw;
try {
joRaw = new JSONObject(raw);
String t=joRaw.getString("d");
System.out.println(t); \\< ----------
JSONObject joD = new JSONObject(t);
return joD;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}