parse complex json in android - android

i have this json:
[{"id":"1","name":"john"},{"id":"2","name":"jack"},{"id":"3","name":"terry"}]
how i can parse this? i have to use a loop for extracting each group? for simple jsons i use this code:
public static String parseJSONResponse(String jsonResponse) {
try {
JSONObject json = new JSONObject(jsonResponse);
// get name & id here
String name = json.getString("name");
String id = json.getString("id");
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
but now i have to parse my new json. please help me

It should be like this:
public static String parseJSONResponse(String jsonResponse) {
try {
JSONArray jsonArray = new JSONArray(jsonResponse);
for (int index = 0; index < jsonArray.length(); index++) {
JSONObject json = jsonArray.getJSONObject(index);
// get name & id here
String name = json.getString("name");
String id = json.getString("id");
}
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
Of course you should return an array of names or whatever you want..

This is meant to be parsed by a JSONArray, and then each "record" is a JSONObject.
You can loop on the array and then retrieve the JSON String of each record with the getString(int) method. Then use this string to build a JSONObject, and just extract values like you do now.

You can use the following code:
public static void parseJSONResponse(String jsonResponse) {
try {
JSONArray jsonArray = new JSONArray(jsonResponse);
if(jsonArray != null){
for(int i=0; i<jsonArray.length(); i++){
JSONObject json = jsonArray.getJSONObject(i);
String name = json.getString("name");
String id = json.getString("id");
//Store strings data or use it
}
}
}catch (JSONException e) {
e.printStackTrace();
}
}
You need to modify the loop to store or use the data.
Hope it helps.

Related

Regd : getting value from json url

I want to get the id ^& content value in http://rest-service.guides.spring.io/greeting
What i tried is,
try {
JSONObject jsonObj = new JSONObject(parsingUrl);
// If you have array
JSONArray resultArray = jsonObj.getJSONArray("id"); // Here you will get the Array
// Iterate the loop
for (int i = 0; i < resultArray.length(); i++) {
// get value with the NODE key
JSONObject obj = resultArray.getJSONObject(i);
String name = obj.getString("content");
}
// If you have object
//String result1 = jsonObj.getString("result");
} catch (Exception e) {
e.printStackTrace();
}
Thanks
The url that your mentioned dont have json arrays, parsing will be like
try {
JSONObject jsonObj = new JSONObject(resultfromUrl);
int id = jsonObj.getInt("id");
String name = jsonObj.getString("content");
} catch (JSONException e) {
e.printStackTrace();
}

No value For JSONstring

This is My Json String
{"Damages":[{"id":15,"rf_no":5,"state":"Print5","dmg_no":0,"town":"NEWASA","date":"16\/08\/2015","firm_name":"SHREE ENTERPRISES (NEWASE) "},{"id":36,"rf_no":7,"state":"Print7","dmg_no":0,"town":"NEWASA","date":"16\/08\/2015","firm_name":"SHREE ENTERPRISES (NEWASE) "}]}
But in Android Application its Showing No Value For Damages
This is My Android Code
JSONArray jArray = new JSONArray(json.getString("Damages"));
for (int i = 0; i < jArray.length(); i++) {
JSONObject c = jArray.getJSONObject(i);
final String id = c.getString("id");
String dmg_no =
c.getString("dmg_no");
String firm_name = c.getString("firm_name");
final String rf_no = c.getString("rf_no");
String town=c.getString("town");
String date=c.getString("date");
String State=c.getString("state");
}
Help Me Please. And Thanx In Advance.
You can try this code, to parse your json data:
try
{
JSONObject jsonObj=new JSONObject(result); // result=JSON string
if(jsonObj.has("Damages"))
{
JSONArray arrayObj=jsonObj.getJSONArray("Damages");
for(int i=0;i<arrayObj.length();i++)
{
JSONObject childArray=arrayObj.getJSONObject(i);
Log.e("", "ID "+childArray.getString("id"));
Log.e("", "Ref No"+childArray.getString("rf_no"));
// similarly you can parse rest of your tags
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Hope this helps you.
This is your mistake : JSONArray jArray = new JSONArray(json.getString("Damages"));
you are trying to get the string by name "Damages" and coverting it to JSONarray. But what you have to do is you have to convert your string to JSONObject first and then get the array named "Damages" from that json object.
Try this
String jsonString = {"Damages":[{"id":15,"rf_no":5,"state":"Print5","dmg_no":0,"town":"NEWASA","date":"16\/08\/2015","firm_name":"SHREE ENTERPRISES (NEWASE) "},{"id":36,"rf_no":7,"state":"Print7","dmg_no":0,"town":"NEWASA","date":"16\/08\/2015","firm_name":"SHREE ENTERPRISES (NEWASE) "}]}
try {
JSONObject jsonObject=new JSONObject(jsonString);
JSONArray damageArray=jsonObject.getJSONArray("Damages");
for(int i=0;i<damageArray.length();i++)
{
JSONObject obj=damageArray.getJSONObject(i);
String dmg_no = obj.getString("dmg_no");
String firm_name = obj.getString("firm_name");
final String rf_no = obj.getString("rf_no");
String town=obj.getString("town");
String date=obj.getString("date");
String State=obj.getString("state");
}
} catch (JSONException e) {
e.printStackTrace();
}
This is the problem line
JSONArray jArray = new JSONArray(json.getString("Damages"));
What are you doing is telling the program to find value of String Damages and then get array with name of that value - but there is no array called like that! Your array is already named, and the name is Damages.
To get the array you simply do
JSONArray jArray = json.getJSONArray("Damages");
assuming json is the main response you are getting

Covnert JSON to string in android

I am trying to get the user details using people.get in which I have emails which I need to get that value.
This is what I mean:
This is how I'm trying to do but unable to get the value
try {
String resp =profile.getEmails().toString();
JSONObject mainObject = new JSONObject(resp);
JSONObject uniObject = mainObject.getJSONObject("emails");
String email = uniObject.getString("value");
((TextView) findViewById(R.id.txtemail)).setText(email);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Any help is appreciated.
In your JSON response, emails is a JSONArray, not a JSONObject. You would need to get it like this:
JSONObject json= new JSONObject(responseString); //your response
try {
JSONArray responseArray = jsonObj.getJSONArray("email");
for (int i = 0; i < responseArray.length(); i++) {
// get value with the NODE key
JSONObject obj = responseArray.getJSONObject(i);
String lastName = obj.getString("value");
String firstName = obj.getString("type");
EmailResponse myResp = new EmailResponse();
myResp.setValue(value);
myResp.setType(type);
//set all other Strings
//lastly add this object to ArrayList<MyResponse> So you can access all data after saving
}
}
catch (JSONException e) {
e.printStackTrace();
}
POJO Class:
public class EmailResponse{
public String value = "";
public String type = "";
//getter setters
}
Hope this helps.

how can decode json data form api in android?

I am geting JSON data getting from web service. Below is my code.
How can I decode the json data?
{
"response": [
{
"last_name": "Test",
"id": 279711390,
"first_name": "Vishnu",
"sex": 2,
"photo_50": "https://vk.com/images/camera_50.gif"
}
]
}
How can I parse it? Thanks.
You can keep a POJO class. With the data which you are about to get from server. And parse them and save in that object.
Example:
JSONObject json= new JSONObject(responseString); //your response
try {
JSONArray responseArray = jsonObj.getJSONArray("response");
for (int i = 0; i < responseArray.length(); i++) {
// get value with the NODE key
JSONObject obj = responseArray.getJSONObject(i);
String lastName = obj.getString("last_name");
String firstName = obj.getString("first_name");
//same for all other fields in responseArray
MyResponse myResp = new MyResponse();
myResp.setFirstName(firstName);
myResp.setLastName(lastName);
//set all other Strings
//lastly add this object to ArrayList<MyResponse> So you can access all data after saving
}
}
catch (JSONException e) {
e.printStackTrace();
}
POJO Class:
public class MyResponse{
public String firstName="";
public String lastName="";
//all other fields and getter setters
}
Hope this helps.
You can parse JSON using this code:
str="<The Json>"
try {
JSONObject jObject=new JSONObject(str);
JSONArray menuObject = new JSONArray(jObject.getString("response"));
String lastName;
for (int i = 0; i<menuObject.length(); i++) {
lastName=menuObject.getJSONObject(i).getString("last_name").toString();
...
}
catch (JSONException e) {
e.printStackTrace();
}
Use this code :-
String string = "Your Json"
try {
JSONObject jsonObject=new JSONObject(str);
JSONArray menuObject = new JSONArray(jObject.getJsonArray("response"));
//no need of for loop because you have only one object in jsonArray.
JSONObject oject = menuObject.getJSONObject(0);
String lastName = object.getString("last_name");
String firstName = object.getString("first_name");
Log.d("User Name", firstName + " " + lastName);
catch (JSONException e) {
e.printStackTrace();
}

parse json without title object in android

i have this json : {"id":1,"name":"john smith"}
How i can parse it? i know i have to do this with this function:
public static String parseJSONResponse(String jsonResponse) {
String name = "";
JSONObject json;
try {
json = new JSONObject(jsonResponse);
JSONObject result = json.getJSONObject("**********");
name = result.getString("**********");
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
but i dont know what can i put in the areas incated with "****". please help me
i only want to fetch id and name values.
parse current json String as:
json = new JSONObject(jsonResponse);
// get name here
name = json.getString("name");
// get id here
id = json.getString("id");
because current json string contain only one jsonObject
use this method to parse your JSON String
public static String parseJSONResponse(String jsonResponse) {
try {
JSONObject json = new JSONObject(jsonResponse);
// get name & id here
String name = json.getString("name");
int id = json.getInt("id");
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
For more Refer this Link

Categories

Resources