I have to pass JSON data in web services. Like below
"customerAccount": {
"payloadType": "KEYED",
"accountType": "CHECKING",
"cardDetails": {
"dataFormat": "PLAIN_TEXT",
"cardNumber": "5454545454545454",
"expiryDate": "1221",
"cvv": "123",
"issueNumber": "01"
}
}
Kindly guide me on how can I create JSON Object.
PS: Kindly not comment for JSONArray(). I have to use JSON Object, not JSONArray.
Do you mean some java json parse framework?
You can try fastjson or jackson.
Related
I am facing strange problem with Google Volley I hope some one help me out quickly. I want to send JSON array in parameter and server will give me response in JSON object. How can we achieve this?
e.g
I wanted to post this JSON array.
[
{
"name":"John",
"age":"30",
"cars":"6"
},
{
"name":"John",
"age":"30",
"car
}
]
and server will send response in JSON object format like this.
{
"status":"success",
"code":30,
}
Can someone explain to me how I can achieve this thing?. Moreover, my JSONarray consist of mobile contacts and size will be large.
You can use Map<Object, Object> where value be collection of json Element you want. You can make custom deserilizator with lib like "gson".
You should try: JSONArray arr = new JSONArray(JSON_STRING);
I have a JSON which is returned from a Web Service Developed in PHP but I need help to converting this JSON to JSONArray in Android
{"Name":"John","Contact":"3331234567"}{"Name":"Doe","Contact":"3017654321"}{"Name":"Smith","Contact":"3001234765"}
How can i convert this String returned from URL to JSONArray
Thank you Guys for giving me an idea to fix this problem. I have figured it out bu conceding a string e.g: "n" while writing response on my web service now i can split this on my Android App.
I'd suggest to return it from the web service as a standard JSON Array, which means:
[{"Name":"John","Contact":"3331234567"},{"Name":"Doe","Contact":"3017654321"},{"Name":"Smith","Contact":"3001234765"}]
In this case you can parse it with any common json parser easily (gson, org.json, any other).
Returning it as you do it now produces invalid JSON - just put it to any online JSON validator and it will complain about this.
1. I suggested you to create json file in standard mode
like this:
{
"USER":[ {
"Name":"John",
"Contact":"3331234567"
}
{
"Name":"Doe",
"Contact":"3017654321"
}
{
"Name":"Smith",
"Contact":"3001234765"
}]
}
if you have not any field more , you can remove "USER":
2. You should use json parser library like GSON
**3.**use "pojo" site for create json model in java like http://www.jsonschema2pojo.org/
I'm very new to Android and having a lot of trouble understanding JSON. I have a separate JSON file with my values written out but no idea how to actually display those strings in an Android app.
Any comprehensive tutorials or basic-level explanations are very welcome. Thanks!
First of all try to understand AsyncTask also. It allows the user to perform a long background operations and to show the results back to the user in the MainUI Thread. You can perform JSON parsing and get values which is stored inside the JSON file via AsyncTask.
JSON is very light, easy to understand and it's the best alternative to XML. To parse your JSON file you need to know about JSONArray and JSONObject. In a JSON file , square bracket [ represents a JSON array and curly braces { represent the JSON objects. JSON is structured with Key and Value pairs. Get your string values with getString("key");and then just display it into TextView
JSONArray - It contains many JSON Objects.
JSONObjects - It contains key and value pairs.
[ -> It represents the JSON Array
{ -> It represents the JSON Object
Two methods getJSONArray() and getJSONObject() are mainly used in the JSON to represent the json node.
Get your JSON Array node.
JSONArray booksArray = jsonObj.getJSONArray("books");
JSON Code :
{
"books": [ //JSON array
{ //represents JSON Object
"id":"440", //Key and Value pair
"edition": "Fourth",
"language": "Java",
},
{
"id":"407",
"edition": "second",
"language": "Python",
}
]
}
Above code has one JSON Object(books) and one JSON Array it holds two json objects. See this for JSON Parsing in Android.
You can also upload your JSON file for free pastebin and view your JSON file as tree structure with JsonViewer
NOTICE : Comments are not officially supported in JSON. So I just added comments like in JAVA.
I have done rounds of JSON parsing but this is something that I have never come accross .How would we parse this kind of json Array?
[{
"about": "xyz ",
"id": "cda",
"username": "ps"
}, {
"description": "this is sample response",
"id": "ahhj",
}]
Clearly, your json is not a list of homogeneous data. Hence, cannot be converted to List.
So, I tried to be in your shoes for a while, and created a json
[{"name":"dave","clazz":"V"},{"token":"VIW","type":"dexter"}]
and the best approach I could come up with
1: Convert it to JsonArray
JSONArray jsonArray = new JSONArray(json);
2: Since each object in array could of unique type, so until you are very sure about it keeping it as JsonObject is not that bad idea.
JsonObject jsonObject = jsonArray.getJSONObject(0);
String token = jsonObject.getString("token");
3: And for the cases when you are sure you have a Model (POJO) for the class you can definately to something like this.
MyPojo myPojo = new Gson().fromJson(jsonArray.getJSONObject(0).toString(), MyPojo.class);
Hope I helped! No! How about you tell me what I did wrong. :)
Maybe this answer might be of help.
Basically you should iterate each JSON object and use a GSON method to get hold of all the keys. Then you could iterate each keys and get the value using the key.
use jsonObject.had("key") to know if this key exist for this jsonObject. So you can identify your object from their unique key like in your example :
if the object have the about key so it's like the first object and if it have description it's the second one. Make sure you iterate all JsonObject in the JsonArray
your Json result to be a JsonObject that has Array of data.
change your result data like below:
"data":{[
{
"about": "xyz ",
"id": "cda",
"username": "ps"
},
{
"description": "this is sample response",
"id": "ahhj",
}
]}
then make a model class of this and use GSON. if you have any problem ask again.
I would probably deserialize it into a Map<String,String>[] or if you are expecting other data types as values Map<String, Object>[] and traverse it that way as needed.
In my app I'm receiving notifications from Facebook using facebook sdk for android. But the data returned by response.toString() method is in json format.
I need to know how can I convert it in simple human readable format so that I can use them to view in a textfield?
This is what I get out of graph explorer.Required string is "title"
{
"id": "10000115768434",
"name": "Nauman Aslam",
"notifications": {
"data": [
{
"id": "notif_10000357346818423_6543370",
"from": {
"name": "Muhammad Asad Iqbal",
"id": "56342467433"
},
"to": {
"name": "Nauman Aslam",
"id": "324545436818423"
},
"created_time": "2014-03-28T20:09:02+0000",
"updated_time": "2014-03-28T20:16:05+0000",
"title": "Muhammad Asad Iqbal and Smat Kazmi also commented on Muhammad Asad Iqbal's photo.",
"link": "http://www.facebook.com/photo.php?fbid=4040070417111&se",
"application": {
"name": "Photos",
"id": "54635732"
},
"unread": 1
},
You will either need to find JSONparser libraries on-line and use them or if you want you can write your own JSONparser class and parse the data in java objects (By the way JSON response is usually human readable its just that it comes as an object or arrays or objects).
if you want to write your own JSONparser class it shouldn't be that hard we can not advise you on where to start until you post the JSONresponse and we see the format of the response.
one things I would say is first check the format of your JSON response. It is very important to know the format because if JSONresponse comes in format of "object of arrays of objects" you must decode it using "JSONObject --> JSONArray ->> JSONObject" otherwise your program will crash. Click here and copy paste your json response on the textbox it will show you the format of it.
If you want to start with basics of JSON decoding Click here and check my answer on stackoverflow its very basic but if you have only started to deal with JSONresponses it might be a good start.
Edit
As per your JSONresponse it starts from curly brackets which means the response is one JSONObject but I do not see the whole JSONresponse and some brackets are missing so my answer might not make sense but you can follow the steps if you understand it
first thing you do is save the response in 'String' object and create a JSONObject and initialise it by passing the response. then you want the the "notifications" object extracted from it so you again create second JSONObject this object will only extract "notifications" object from the string
then you want the JSONArray "data" to be extracted so you define a JSONArray and getJSONArray("data") from you JSONObject.
and thei again you will need JSONObject to only extract "title" object.
to summarise
Create JSONObject and pass the response String to it
Create second JSONObject from it
Create JSONArray and use getJSONArray("___") method on you JSONObject
and finally create last JSONObject inside a for loop if you have more than one notification
and use getString("title") method on that object
Your JSON response is not complete so I might be wrong but you can give it a try.
When you get a response string in json format there are libraries that allows you to turn your string in a dictionary type.
If you want a web site that helps you view a json string in readable by hummans go here Online JSON Viewer.
Paste your code in the first page then go to viewer tab, you'll see the name of the keys and the values.