{
"id": "2345631223",
"name": "MyFbAcc",
"picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4 /373042_396504583708761_162084_s.jpg",
"link": "http://www.facebook.com/MyFbAcc",
"likes": 70,
"cover": {
"cover_id": 434873094856,
"source": "http://a3.sphotos.ak.fbcdn.net/hphotos-ak-snc7/s720x720/396014_493579484001270_1795_n.jpg",
"offset_y": 0
}
Normal Xml files I used to read xml parsing/DocumentBuilderFactory, which xml consists of tags, etc, but here all are represent in " ". how do I get read the id value?
id=2345631223. Any help most welcome?
Using JSON..
JSONObject object = new JSONObject(yourString);
String id = object.getString("id");
if i suppose you are making the JSON object with a String
String jString="{"name":"value"};
JSONObject jObject = new JSONObject(jString);
then you can retrieve the value form the name-value pairs like
String valueFromJSON= jObject.getString("name");
Related
I have a JSONObject. I need to append some details to that.
Existing JSON
{
"class": {
"name": "first",
"language": "English"
}
}
and i need to append values to this like
{
"class": [{
"name": "first",
"language": "English"
}, {
"name": "first",
"language": "English"
}]
}
I'm not the first to ever say this,
But you should use JSONObject.put with the modified value, e.g
JSONObject main = new JSONObject();
// ...
main.put("Classes", newClasses);
You can't do that with your current object. You have to change the type of the JSONObject you're appending to. As noted in the comments, you actually just want to serialize an array of custom objects.
Therefore, create a JsonArray as described in this answer: JSONObject.append into object - result is nested array?
Java is not my first language, but maybe this will help you out:
//First create the name and language object
JSONObject nameLanguageObj = new JSONObject();
nameLanguageObj.add("name","first");
nameLanguageObj.add("language","English");
//Create the array to hold the objects
JSONArray arr = new JSONArray();
arr.put(nameLanguage);
//Create the full class object as you requested.
JSONObject classObj = new JSONObject();
classObj.put("class", arr);
I am sending request to the server and it gives me a response and giving data in JSON formats, now i want to fetch some specific value from that JSON format, so hot to do it.
{
"education": [
{
"school": {
"id": "2009305",
"name": "FG boys public high school Bannu Cantt "
},
"type": "High School"
},
{
"school": {
"id": "109989",
"name": "University of Engineering & Technology"
},
"type": "College"
}
],
"id": "xxxxxxx"
}
Now i need the school names from this xml,
Try this..
You response is in Json format not xml.
JSONObject json = new JSONObject(response);
JSONArray education = json.getJSONArray("education");
for(int i = 0; i < education.length(); i++){
JSONObject con_json = education.getJSONObject(i);
String school_type = con_json.getString("type");
JSONObject school_json = con_json.getJSONObject("school");
String school_name = school_json.getString("name");
}
It is not XML. it is completely in Json Standard format.
first build a JSONobject from your data:
JSONObject jsonObj = new JSONObject(result); //result = your Data String in fetched from server
then you cab retrieve what you want using its key. for example:
jsonObj.getString("id"); // it returns "xxxxxxx". as is in your data
So I am receiving a JSON array in an httpresponse. The array consists of the following objects/variables.
{"sessid":"vxkEXkMBUmBByESRlvaxrxSaFTfhDqd8","session_name":"SESS88cdfb2f1c420898","user":{"uid":"60","name":"abc","theme":"","signature":"","signature_format":"filtered_html","created":"13082976","access":"1386287","login":1386211,"status":"1","timezone":null,"language":"","picture":null,"data":{"mimemail_textonly":0},"roles":{"2":"authenticated user","5":"centre user"},"field_centre_reference":{"und":[{"nid":"256"}]},"field_first_name":{"und":[{"value":"web","format":null,"safe_value":"web"}]},"field_surname":{"und":[{"value":"services","format":null,"safe_value":"services"}]},"bounce_mail_blocked":false,"force_password_change":"0"}}
Now I want to receive all these objects/strings in separate variables. Like i want to store the "sessid" in a variable String session_id. And so on. I can get the first two (i.e. sessid and session_name) in a simple way with the help of the following code.
response = client.execute(httppost);
BasicResponseHandler handler = new BasicResponseHandler();
String data = handler.handleResponse(response);
jObj = new JSONObject(data);
sessid = jObj.getString("sessid");
Log.d("sessid obj", sessid);
session_name = jObj.getString("session_name");
Log.d("session_name", session_name);
But since I am a noob at Android, I don't know how to get the rest of the data to be saved in variables. The upcoming data cannot be saved in a simple way.
Try with this:
JSONObject j_user = jObj.getJSONObject("user");
String uid = j_user.getString("uid");
...
//And so on with the rest of the fields
{ // json object node
"sessid": "vxkEXkMBUmBByESRlvaxrxSaFTfhDqd8",
"session_name": "SESS88cdfb2f1c420898",
"user": { // json object user
"uid": "60", // string
"name": "abc",
"theme": "",
"signature": "",
"signature_format": "filtered_html",
"created": "13082976",
"access": "1386287",
"login": 1386211,
"status": "1",
"timezone": null,
"language": "",
"picture": null,
"data": { // json object data
"mimemail_textonly": 0 //string
},
....// rest of the json
{ represents json object node
[ represents json array node
To parse
JSONObject jObj = new JSONObject(load());
String sessid = jObj.getString("sessid");
Log.d("sessid obj", sessid);
JSONObject user = jObj.getJSONObject("user");
String uid = user.getString("uid");
Log.d("sessid obj", uid);
To parse data
"data": {
"mimemail_textonly": 0
},
JSONObject data= user.getJSONObject("data");
Log.i(".......",""+data.getString("mimemail_textonly"));
To parser field_centre_reference
"field_centre_reference": { // json object field_centre_reference
"und": [ // json array und
{ // json object node
"nid": "256" //string
}
]
},
JSONObject field= user.getJSONObject("field_centre_reference");
JSONArray jr = field.getJSONArray("und");
JSONObject jb1 = (JSONObject) jr.get(0);
Log.i(".......",""+jb1.getString("nid"));
Check out JacksonParser library, it provides annotations which make your work so easy. And it is one of fastest parsing libraries... You can have it here
Edit:
As an example you can take a look at one of my previous question JacksonParser databind and core cause "Found duplicate file for APK"?
You should notice that the JSON you received is a nested JSON. It is like a Map that one of the value of Map is also a Map.
In your case, there is a JSONObject contains sessid and session_name while is only string, but the "user" is also a JSONObject, so you can parse it level by level.
For more details, you can read http://www.vogella.com/articles/AndroidJSON/article.html
Try this..
Getting
"user": {
"uid": "60",
JSONObject obj_user = jObj.getJSONObject("user");
String uid = obj_user.getString("uid");
"user": {
"uid": "60",
"data": {
"mimemail_textonly": 0
}
JSONObject obj_data = jObj.getJSONObject("data");
String mimemail_textonly = obj_user.getString("mimemail_textonly");
JSONObject obj_roles = jObj.getJSONObject("roles");
String two = obj_roles.getString("2");
"field_centre_reference": {
"und": [
{
"nid": "256"
JSONObject obj_field_centre_reference = jObj.getJSONObject("field_centre_reference");
JSONArray ary_und = obj_field_centre_reference.getJSONArray("und");
JSONObject objve = ary_und.getJSONObject(0);
String nid= objve.getString("nid");
How can I parse the following to retrieve the following.
{"docs": [
"SolrDocument[{content_type=[text/plain], description=/mnt/sdcard/A.txt, id=4df6fa43-1a99-4475-bc5b-80dd0ab6d114, name=uploadedfile}]",
"SolrDocument[{content_type=[text/plain], description=asdf.png, id=9fb20d5d-cf39-4635-9a22-64560124809e, name=uploadedfile}]"]
}
I need to retrieve description into a string array and id to another string array.
I tried
a="docs"
JSONObject jobj = new JSONObject(line);
JSONArray IDS = (JSONArray) jobj.get(a);
This works but it returns an array of strings
The data you are trying to parse isn't valid JSON, so you are now in RegEx territory. So reading each line out of the JSONArray you could extract what you are after like this:
String data = "SolrDocument[{content_type=[text/plain], description=/mnt/sdcard/A.txt, id=4df6fa43-1a99-4475-bc5b-80dd0ab6d114, name=uploadedfile}]";
Pattern values = Pattern.compile("(description|id)=([\\S]+),", Pattern.CASE_INSENSITIVE);
Matcher matches = values.matcher(data);
while(matches.find()) {
System.out.println(matches.group(1) + "=" + matches.group(2));
}
This will print out:
description=/mnt/sdcard/A.txt
id=4df6fa43-1a99-4475-bc5b-80dd0ab6d114
The elements inside the JSONArray "docs" are not valid JSON objects. You must have "key":"value" pairs in order to be valid.
For example:
{
"docs": [
{
"id": 912345678901,
"text": "How do I read JSON on Android?",
"geo": null,
"user": {
"name": "android_newb",
"followers_count": 41
}
},
{
"id": 912345678902,
"text": "#android_newb just use android.util.JsonReader!",
"geo": [
50.454722,
-104.606667
],
"user": {
"name": "jesse",
"followers_count": 2
}
}
]
}
There are plenty of sites that can validate your JSON, for example JSONLint
Before we can answer your question, fix your JSON.
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.