android json starting with string [duplicate] - android

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 6 years ago.
returnLogin([{
"id": 0,
"first_name": "abc",
"last_name": "xyz",
"msg": "Invalid User or Location ID or Password.",
"location_id": "",
}])
How to get data from above Json string?
here json start with witn string not any bracket thats why i try to. getting data

you can parse like below,
String data = "";// here your json data which will parse
try {
JSONArray jsonArray = new JSONArray(data);
JSONObject jsonObject=jsonArray.getJSONObject(0);
int id=jsonObject.getInt("id");
String first_name=jsonObject.getString("first_name");
String last_name=jsonObject.getString("last_name");
String msg=jsonObject.getString("msg");
String location_id=jsonObject.getString("location_id");
} catch (JSONException e) {
e.printStackTrace();
}
But before parse correct your data.

Related

How to parse this json array [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 5 years ago.
How to fetch Keys inside date arrays i am able to parse till '2'how to get that 19:00:00
"date_times": {
"2": {
"2018-01-08": [
**"19:00:00"**
],
"2018-01-09": [
"13:30:00",
"19:00:00"
],
"2018-01-10": [
"13:30:00",
"19:00:00"
],
"2018-01-11": [
"13:30:00",
"19:00:00"
]
}
}
If you concern about getting keys from jsonobject . Then just use an Iterator on keys.
try {
JSONObject resObject = new JSONObject(response);
Iterator<String> iterator = resObject.keys();
while (iterator.hasNext()) {
String key = iterator.next();// this will be your date
JSONArray data = resObject.getJSONArray(key);// And this is your data
}
} catch (Exception e) {
}
This is just an example modify it as per your need

How to parse JSON array in android?[JSON structure below in description] [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 6 years ago.
I have an Json Array Like -
[
500,
"Applied Before"
]
How can I parse it in android.
You can try like this
String data = " [500," +
" \"Applied Before\"] ";
try {
JSONArray arr = new JSONArray(data);
Log.i("arr",""+arr.get(0));
} catch (JSONException e) {
e.printStackTrace();
}
Improper JSON Array syntax .
Example of a JSON Array :
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter","lastName":"Jones"}
]

How to read Json data in android [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a raw data as in a below format:
[
{
"id": "1",
"name": "abc",
"type": "consumer"
},
{
"id": "2",
"name": "cdf",
"type": "consumer"
},
{
"id": "3",
"name": "jok",
"type": "owner"
}
]
Please let me know how can I covert that into JsonArray and get the each values.
This is the simplest way to parse your JSON String. I would suggest your to read JSON documents.
But, here is a sample code.
try {
String jsonString = new String("[{\"id\": \"1\",\"name\": \"abc\",\"type\": \"consumer\"}]");
JSONArray jsonArray = new JSONArray(jsonString);
for(int index = 0;index < jsonArray.length(); index++) {
JSONObject jsonObject = jsonArray.getJSONObject(index);
String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String type = jsonObject.getString("type");
}
} catch (JSONException e) {
e.printStackTrace();
}
You can also parse using GSON https://code.google.com/p/google-gson/

Parsing JSON object in android

I have JSON object as follows:
[
{
"Project": {
"id": "1",
"project_name": "name"
},
"AllocationDetail": [
{
"id": "1",
"project_id": "1",
"team_name": "ror",
"week_percentage_work": "50",
"in_today": "1",
"actual_hours": "30",
"remaining_hours": "100",
"time_difference": null,
"created": "2012-01-13 15:48:33",
"modified": "2012-01-13 15:48:33"
},
{
"id": "2",
"project_id": "1",
"team_name": "php",
"week_percentage_work": "40",
"in_today": "2",
"actual_hours": "50",
"remaining_hours": "100",
"time_difference": null,
"created": "2012-01-13 15:49:40",
"modified": "2012-01-13 15:49:40"
}
]
}
]
I want to parse data in android and store it into DB, but i m getting confused in Jsonobject
thanks
the best JSON Tutorial i have seen, I leared json parsing from this tutorial, hope it will help
The following is a snippet code for parsing your json string. Kindly go through it:
String response = <Your JSON String>;
String Project = null;
String AllocationDetail = null;
try {
JSONArray menuObject = new JSONArray(response);
for (int i = 0; i< menuObject.length(); i++) {
Project = menuObject.getJSONObject(i).getString("Project").toString();
System.out.println("Project="+Project);
AllocationDetail = menuObject.getJSONObject(i).getString("AllocationDetail").toString();
System.out.println("AllocationDetail="+AllocationDetail);
}
JSONObject jsonObject = new JSONObject(Project);
String id = jsonObject.getString("id");
System.out.println("id="+id);
String project_name = jsonObject.getString("project_name");
System.out.println("project_name="+project_name);
JSONArray jArray = new JSONArray(AllocationDetail);
for (int i = 0; i< jArray.length(); i++) {
String team_name = jArray.getJSONObject(i).getString("team_name").toString();
System.out.println("team_name="+team_name);
String week_percentage_work = jArray.getJSONObject(i).getString("week_percentage_work").toString();
System.out.println("week_percentage_work="+week_percentage_work);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
There are two ways for parsing JSON
Google GSON
JSON
When your content of JSON is too complex and long enough you can prefer usin GSON its much easier to maintain than JSON parsing each value manually.
Use jsonlint.com to read your json better. It appears that the json that you have copied here is invalid.
String response = <Your JSON String>; // add your Json String here
response = response.replace("[","").replace("]",""); // Just remove all square braces
JSONObject json = new JSONObject(response); //Convert the Json into Json Object
JSONObject jProject = json.getJSONObject("Project"); // get your project object
String project_id = jProject.getString("id"); // Get the value of Id
String project_name = jProject.getString("project_name"); // get the value of Project_name
The above code can be use many times as you want to parse the Json.
I know its too late, but It may help many people who are trying to find the same answer.
I was trying to find the same solution, but it was too hard to use the Accepted answer because of too many lines of codes, But I got the same results with hardly few lines of codes.

How to parse json string in Android? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JSON Array iteration in Android/Java
I am fetching JSON string from server and I have already got JSON string by code.
But I didn't understand how to parse it.
Below is my JSON string
{
"university": {
"name": "oxford",
"url": "http://www.youtube.com"
},
"1": {
"id": "2",
"title": "Baseball",
"datetime": "2011-11-11 10:41:46"
},
"2": {
"id": "1",
"title": "Two basketball team players earn all state honors",
"datetime": "2011-11-11 10:40:57"
}
}
Please provide any guidance or code snippet.
Use JSON classes for parsing e.g
JSONObject mainObject = new JSONObject(Your_Sring_data);
JSONObject uniObject = mainObject.getJSONObject("university");
String uniName = uniObject.getString("name");
String uniURL = uniObject.getString("url");
JSONObject oneObject = mainObject.getJSONObject("1");
String id = oneObject.getString("id");
....
Below is the link which guide in parsing JSON string in android.
http://www.ibm.com/developerworks/xml/library/x-andbene1/?S_TACT=105AGY82&S_CMP=MAVE
Also according to your json string code snippet must be something like this:-
JSONObject mainObject = new JSONObject(yourstring);
JSONObject universityObject = mainObject.getJSONObject("university");
JSONString name = universityObject.getString("name");
JSONString url = universityObject.getString("url");
Following is the API reference for JSOnObject: https://developer.android.com/reference/org/json/JSONObject.html#getString(java.lang.String)
Same for other object.

Categories

Resources