Printing json object on an android textview - android

I have this json object as an output of a web service , I want to print each part_Name node on a separate android text view how to do that?
{
"0": [],
"1": {
"Part_ID": "1",
"Part_NAME": "part_name_one "
},
"2": {
"Part_ID": "2",
"Part_NAME": " part_name_two "
},
"3": {
"Part_ID": "3",
"Part_NAME": "part_name_three"
},
I tried this code but I don't get an output in my textview
jobj = jsonparser.makeHttpRequest("http://192.168.1.7:89/My_website/My_Webservice.php");
try {
JSONObject jsonRootObject = new JSONObject(jobj.toString());
JSONArray jsonArray = jsonRootObject.optJSONArray("Services_Parts");
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("Part_NAME").toString();
data += "Node"+i+ "+ name +" +name+ " \n ";
tv2.setText(name);
}

if possible then try to change the structure of your json to something like that
[
{
"Part_ID": "1",
"Part_NAME": "part_name_one "
},
{
"Part_ID": "2",
"Part_NAME": " part_name_two "
},
{
"Part_ID": "3",
"Part_NAME": "part_name_three"
}
]
put each json object into an jsonarray .. as shown above
then parse each jsonobject and create the textview dynamically acc. to no. of jsonobject inside jsonarray
using the above ex. there are 3 jsonobject inside jsonarray
hence create 3 textview dynamically at the time of json parsing
JSONArray jsonArray = <your json array>;
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("Part_NAME").toString();
addTextView(name);
}
// add text to dynamically created textview code..
public void addTextView(String text)
{
final TextView textView = new TextView(this);
textView.setText(text);
myLinearLayout.addView(textView);
}

Related

Set value inside table from database using array

I am trying to fetch data from MySQL database and trying to show it into a table. For this I am using volley.
JSONArray result = response.getJSONArray("result");
for (int i = 0; i < result.length(); i++) {
JSONObject jsonObject = result.getJSONObject(i);
String name = jsonObject.getString("name");
String phone = jsonObject.getString("phone");
String email = jsonObject.getString("email");
String j = String.valueOf(i);
data = new String[][]{{j, name, email, phone}};
tableView.setDataAdapter(new SimpleTableDataAdapter(getActivity(), data));
}
My json array
{
"result": [
{
"sl": "36",
"user_id": "575512",
"email": "m#gmail.com",
"password": "123",
"name": "Moderator",
"gender": "Male",
"phone": "1223345",
"lvl": "Moderator",
"stat": "1"
},
{
"sl": "68",
"user_id": "814769",
"email": "m2#gmail.com",
"password": "1WCcvnhp",
"name": "Test Moderator",
"gender": "Male",
"phone": "7412589630",
"lvl": "Moderator",
"stat": "1"
}
]
}
The problem is I am getting only the last value from jsonArray, because the String array is resetting itself.
data = new String[][]{{j, name, email, phone}};
I want to get all the values available in the jsonArray.
I am using the following dependencies for table
de.codecrafters.tableview:tableview:2.8.0
Try like this
JSONArray result = response.getJSONArray("result");
for (int i = 0; i < result.length(); i++) {
JSONObject jsonObject = result.getJSONObject(i);
String name = jsonObject.getString("name");
String phone = jsonObject.getString("phone");
String email = jsonObject.getString("email");
String j = String.valueOf(i);
data = new String[][]{{j, name, email, phone}};
}
tableView.setDataAdapter(new SimpleTableDataAdapter(getActivity(), data));
After getting all values in array, add that array to tableview

how to parse this type json array in android [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 5 years ago.
this is array which i have
[ {
"comment": "Sir, 2nd paper qualifying hai ya Uski bhi merit banegi?",
"user": "Sameer",
"image": "https://jdcivils.org/images/co.png",
"date": "04/Sep/2016",
"reply": [
{
"comment": "Abhi qualifying nhi hai,iske marks merit list me judte hain",
"user": "Admin",
"image": "https://jdcivils.org/images/co.png",
"date": "04/Sep/2016"
}
]
},
{
"comment": "Cgpsc me hight kis kis post ke liye jaruri hoti hai..\r\nplss btayega koi??",
"user": "Vinod kumar Yadav",
"image": "https://jdcivils.org/images/co.png",
"date": "29/Aug/2016",
"reply": ""
},
{
"comment": "Sir Cgpsc Me gen. category walo Ko Psc k attempts ki koi limit Hoti Hai kya??",
"user": "Vinod kumar Yadav",
"image": "https://jdcivils.org/images/co.png",
"date": "28/Aug/2016",
"reply": [
{
"comment": "nahi",
"user": "Admin",
"image": "https://jdcivils.org/images/co.png",
"date": "28/Aug/2016"
}
]
}]
Try in this way
JSONArray jsonarray = new JSONArray(yourresponse))
for(int i=0;i<jsonarray.length;i++){
JSONObject jsonobject = jsonarray.getJsonObject(i);
String comment = jsonobject.getString("comment");
String user = jsonobject.getString("user");
String image = jsonobject.getString("image");
String date = jsonobject.getString("date");
JSONArray jsonarray1 = jsonobject.getJSONArray("reply");
for(int j=0; j<jsonarray1.length;j++){
String comment1 = jsonobject.getString("comment");
String user1 = jsonobject.getString("user");
String image1 = jsonobject.getString("image");
String date1 = jsonobject.getString("date");
}
}
I am assuming you are getting string in respose so convert it to jsonArray
JSONArray SamplejsonArray = new JSONArray(String);
for(int i = 0;i<SamplejsonArray .length();i++){
JSONObject SamplejsonObject = SamplejsonArray .getJsonObject(i);
String comment = SamplejsonObject .getString("comment");
String user = SamplejsonObject .getString("user");
String image= SamplejsonObject .getString("image");
String date= SamplejsonObject .getString("date");
JSONArray replyjsonArray = SamplejsonObject.getJSONArray(reply);
for(int j = 0;i<replyjsonArray .length();j++){
JSONObject replyjsonObject = replyjsonArray.getJsonObject(j);
String comment = replyjsonObject .getString("comment");
String user = replyjsonObject .getString("user");
String image= replyjsonObject .getString("image");
String date= replyjsonObject .getString("date");
}
}
Use json to pojo converter for create model class from complex json array
Refer link : http://www.jsonschema2pojo.org/
Use this library, all you need is create model with same key as string that you have. Gson parser
<Your model> value = new Gson().fromJson(new String(<Ur string>.getBytes("ISO-8859-1"),
"UTF-8"), <Your model>.class);
try this way
JSONArray jsonArray = new JSONArray(sampleString);
for(int i = 0;i<jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJsonObject(i)
String comment = jsonObject.getString("comment");
String user = jsonObject.getString("user");
JSONArray replyArray = jsonObject.getJSONArray("reply");
for(int j = 0;j<replyArray.length();j++){
JSONObject innerObject = replyArray.getJSONObject(j);
String comment = innerObject.getString("comment"); ...
}
}
try {
//Create array object from String object that contains your json array
JSONArray jArray = new JSONArray(jsonString);
//Get JSONObject from array
jArray.get(0);
} catch (JSONException e) {
e.printStackTrace();
}

How i can access this Json from my AsyncTask?

I'm trying to retrieve data from Json Like below :
{
comments: [
{
id: 78,
comment_user_id: 81,
comment_is_approve: 1,
comment_ads_id: 373,
comment_text: "commmmmmeeeent here ",
created_at: "2017-03-19 08:32:17",
updated_at: "2017-03-19 08:32:17",
user: {
id: 81,
first_name: "name",
last_name: "",
age: "",
email: "l#mail.com",
telephone: "234234234",
}
}
]
}
and here is my asyncTask() :
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url + 373, "GET", params);
// Check your log cat for JSON reponse
//Log.d("All Comments: ", json.toString());
try {
JSONArray comments = json.getJSONArray("comments");
// looping through All Comments
for (int i = 0; i < comments.length(); i++) {
JSONObject c = comments.getJSONObject(i);
// Storing each json item in variable
String id = c.getString("id");
String commentText = c.getString("comment_text");
String name = "";
String phone = "";
Log.i(TAG, "doInBackground Items: " + id + " , "+ commentText);
//Loop through All user details
JSONArray arrUser = c.getJSONArray("user");
int l = 0;
JSONObject user = arrUser.getJSONObject(l++);
name = user.getString("first_name");
phone = user.getString("telephone");
// creating new HashMap
Log.i(TAG, "doInBackground Items: " + name +", " + commentText + ", " + phone);
// adding HashList to ArrayList
mapItems = new HashMap<>();
// adding each child node to HashMap key => value
mapItems.put("id", id);
mapItems.put("first_name", name);
mapItems.put("comment_text", commentText);
mapItems.put("telephone", phone);
contactList.add(mapItems);
I can get commentText and id , but i can't get any data from user array ?
should i add parantethes to users or how i can achieve that ?
Here user is not an array..it is a JsonObject
try this:
JSONObject User = c.getJSONObject("user");
String id = User.getString("id");
String first_name = User.getString("first_name");
or modify your json response to return an array instead.
Also your JSON response is invalid.
this is the valid JSON response:
{
"comments": [{
"id": 78,
"comment_user_id": 81,
"comment_is_approve": 1,
"comment_ads_id": 373,
"comment_text": "commmmmmeeeent here ",
"created_at": "2017-03-19 08:32:17",
"updated_at": "2017-03-19 08:32:17",
"user": {
"id": 81,
"first_name": "name",
"last_name": "",
"age": "",
"email": "l#mail.com",
"telephone": "234234234" //remove comma here
}
}]
}
you can check valid JSON from HERE
you have to apply nested structure like this
JSONArray arrUser1 = c.getJSONArray("user");
for (int j = 0; j < arrUser1.length(); j++) {
JSONObject c1 = arrUser1.getJSONObject(j);
name = c1.getString("first_name");
phone = c1.getString("telephone");
mapItems = new HashMap<>();
mapItems.put("id", id);
mapItems.put("first_name", name);
mapItems.put("comment_text", commentText);
mapItems.put("telephone", phone);
contactList.add(mapItems);
}
User is a JSONObject in this case. I am not sure why you have used a JSONArray.
This should be enough.
JSONObject user = c.getJSONObject("user");
name = user.getString("first_name");
phone = user.getString("telephone");

How to convert json object into string in Android

My JSON format is:
[
{
"change": 1.59,
"name": "ABC",
"price": 10.52,
"volume": 230
},
{
"change": -0.05,
"name": "DEF",
"price": 1.06,
"volume": 1040
},
{
"change": 0.01,
"name": "GHI",
"price": 37.17,
"volume": 542
}
]
I want to parse it and convert it into string. I am using this method for converting it:
JSONObject jsonObj = new JSONObject(jsonStr);
for (int i = 0; i < jsonObj.length(); i++)
{
String change = jsonObj.getString(TAG_CHANGE);
String name = jsonObj.getString(TAG_NAME);
String price = jsonObj.getString(TAG_PRICE);
String volume = jsonObj.getString(TAG_VOLUME);
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_CHANGE, change);
contact.put(TAG_NAME, name);
contact.put(TAG_PRICE, price);
contact.put(TAG_VOLUME, volume);
// adding contact to contact list
contactList.add(contact);
}
But I get an error:
/System.err(867): at org.json.JSON.typeMismatch(JSON.java:111)
How do I resolve this?
Please try it, It should work
JSONArray jsonObj = new JSONArray(jsonStr);
for (int i = 0; i < jsonObj.length(); i++) {
JSONObject c = jsonObj.getJSONObject(i);
String change = c.getString(TAG_CHANGE);
String name = c.getString(TAG_NAME);
String price = c.getString(TAG_PRICE);
String volume = c.getString(TAG_VOLUME);
HashMap < String, String > contact = new HashMap < String, String > ();
contact.put(TAG_CHANGE, change);
contact.put(TAG_NAME, name);
contact.put(TAG_PRICE, price);
contact.put(TAG_VOLUME, volume);
contactList.add(contact);
}
Try this..
You are getting response as JSONArray but you are doing it as JSONObject
[ // this is JSONArray
{ // this is JSONObject
Change this
JSONObject jsonObj = new JSONObject(jsonStr);
to
JSONArray jsonObj = new JSONArray(jsonStr);
You are parsing in wrong way. your json is starts with an array containing JsonObjects. This can be identified since square braces denote JsonArray while curly braces denote JsonObject. Start with:
JSONArray jsonArr = new JSONArray(jsonStr);
then iterate through each object, get JsonObject at each index and use getString method for each key to get values.

parsing a JSON array from a string

can some one please get me the json parsing for an json array
line = "contexts": [
{
"uuid": "6686feaa-a254-42ec-a662-c36f70f7a586",
"name": "School",
},
{
"uuid": "bd8e6c44-d461-4bbe-8946-a3717dc7fa7f",
"name": "Teaching",
}]
I need uuid and name into to string arrays. I tried
String[] x = new String[10];
String[] y = new String[10];
JSONArray jArray = new JSONArray(line);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
x = json_data.getString("name");
y = json_data.getString("uuid");
}
I get type mismatch error when I run this. The type of line is string which I return from server.
use i to add elements to Array's:
String[] x = new String[10];
String[] y = new String[10];
JSONObject json=new JSONObject(line);
JSONArray jArray =json.getJSONArray("contexts");
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
x[i] = json_data.getString("name");
y[i] = json_data.getString("uuid");
}
you can use http://jsonviewer.stack.hu/ for checking json is valid or not. and use ArrayList instead of Array for getting items dynamically from web service
Its neither JSONObject, nor JSONArray, return string from server like below:
line = {"contexts": [
{
"uuid": "6686feaa-a254-42ec-a662-c36f70f7a586",
"name": "School",
},
{
"uuid": "bd8e6c44-d461-4bbe-8946-a3717dc7fa7f",
"name": "Teaching",
}]}
and then parse like below code:
JSONObject json=new JSONObject(line);
JSONArray jArray =json.getJSONArray("contexts");

Categories

Resources