How to get JSONObject without JSONArray - android

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();
}

Related

why is a JSON file not getting read by my app?

I am new to Stackoverflow and this is my first question.
I am developing an application where I am reading data from a json file from assets folder and storing the values in array list.
here is my code
try {
JSONObject object=new JSONObject(loadJSONFromAsset());
String objec=object.getString("root");
JSONObject object1=new JSONObject(objec);
JSONArray array=object1.getJSONArray("child");
for (int i=0;i<array.length();i++)
{
JSONObject jsonObject=array.getJSONObject(i);
list.add(String.valueOf(jsonObject));
}
} catch (JSONException e) {
e.printStackTrace();
}
// Inflate the layout for this fragment
return rootview;
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("data.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
And this is my json data structure
"root" : {
"child" : [ "data1","data2","data3",.......]},
I want the data1,data2 values inside a list
Try this
try {
JSONObject object=new JSONObject(loadJSONFromAsset());
String objec=object.getString("root");
JSONObject object1=new JSONObject(objec);
JSONArray array=object1.getJSONArray("child");
for (int i=0;i<array.length();i++)
{
list.add(array.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
try {
JSONObject object=new JSONObject(loadJSONFromAsset());
String objec=object.getString("root");
JSONObject object1=new JSONObject(objec);
JSONArray array=object1.getJSONArray("child");
for (int i=0;i<array.length();i++)
{
JSONObject jsonObject=array.getJSONObject(i);
list.add(jsonObject.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}

How to access volley response like this

How to access volley response {"data":"success"} like this.
i am already try this
JSONObject j = null;
try {
j = new JSONObject(response);
result = j.getJSONArray("data");
} catch (JSONException e) {
e.printStackTrace();
}
You're trying to access a JSONArray from a JSONObject while it's just a string. So just replace getJSONArray with getString and try this
String data = new JSONObject(response).getString("data");
Or in your code
try {
JSONObject j = new JSONObject(response);
result = j.getString("data");
}
catch (JSONException e) {
e.printStackTrace();
}
Access Like This
JSONObject j = null;
try {
j = new JSONObject(response);
result = j.getString("data");
} catch (JSONException e) {
e.printStackTrace();
}
You can check this, before retrieving check if that tag (i.e. "data" for your case) is exists on that json object.
JSONObject j = null;
try {
j = new JSONObject(response);
if(j.has("data") {
result = j.getString("data");
}
} catch (JSONException e) {
e.printStackTrace();
}
As the value of data is string and to get string value you have to use JSONObject.getString("data")
Use:
result = j.getString("data");
Instead of:
result = j.getJSONArray("data");

How to handle null response for json array in android

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();
}

Parsing Specific JSON data from website and Displaying in TextView

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.

Parsing asmx returned JSON on Android

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;
}
}

Categories

Resources