I am not able to post Json array from my add to cart list using retrofit. User will choose the products and then add to cart. Ineed save that data using sqlite db. Final order confirm state - I want to send all products (list) to server like this format code. I searched on google and can't find any solution.
JsonArray datas = new JsonArray();
JsonObject object = new JsonObject();
[
{
"name": "Phone",
"quantity": 50,
"price": 30000
},
{
"name": "Computer",
"quantity": 10,
"price": 15000
}
]
this is very simple just do like this:-
#POST("yourApiEndPoint")
Call<YourPojoClass> yourMethodName(#Query("data") JSONArray jsonArray);
Related
I am trying to consume below record using JSON with Volley, but it is not a JSONArray, How can I change it to use it in :
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Mean while, The URL in the record seems not correct. How to remove extar forward slashes from url or I can use it to fetch the JPG image?
{
"ID": "131",
"Lead": "",
"Title": "\u06f2\u06f0 \u06af\u0644 \u0641\u0648\u0642 \u0627\u0644\u0639\u0627\u062f\u0647 \u0631\u0648\u0646\u0627\u0644\u062f\u0648 \u062f\u0631 \u0644\u06cc\u06af \u0642\u0647\u0631\u0645\u0627\u0646\u0627\u0646",
"ContentTime": "09:56",
"TypeContent": "public",
"PTime": "1395\/05\/09 - 11:47",
"Content": " <\/div>",
"Tags": "\u0641\u0648\u062a\u0628\u0627\u0644#\u0631\u0648\u0646\u0627\u0644\u062f\u0648#\u0648\u0631\u0632\u0634\u06cc#######",
"Price": "0",
"GID": "106",
"GTitle": "\u0648\u0631\u0632\u0634\u06cc",
"PicURL": "http:\/\/video.dmedia.ir\/images\/news\/131\/thumb_131.jpg",
"comment_count": "0",
"view_count": "0",
"Media": [{
"GID": "359",
"GType": "mp4",
"Title": "",
"URL": "http:\/\/video.dmedia.ir\/images\/news\/131\/media\/359.mp4",
"ADV": 0
}],
"smscontent": null,
"Rels": [{
"RelID": "130",
"RelTitle": " \u0645\u0647\u062f\u0648\u06cc: \u062f\u0648\u0633\u062a \u062f\u0627\u0631\u0645 \u0648\u0627\u0644\u06cc\u0628\u0627\u0644\u0645 \u0628\u0627 \u06cc\u06a9 \u062e\u0627\u0637\u0631\u0647 \u062e\u0648\u0628 \u062a\u0645\u0648\u0645 \u0634\u0648\u062f"
}],
"Comments": []
}
Solution:
We can make JSON object request and JSON array request:
I found this helpful.
http://www.androidhive.info/2014/09/android-json-parsing-using-volley/
hi there is one plugin Gson for android studio.You need to install.Then go to CTRL + insert.
You can create gson file.
Enter some name for java file.
Click that file then Paste you json data. Click ok.
You can see your created json to gson format.
thanks hope this will help you.
create model class for this data then use follwing code
ModelClass class=new GSON.fromJSON("your Data ", ModelClass.class )
I am using AndroidHive method to display a custom expandable listview. It has a static data inserted. but in my app , i am receiving a json data and i am unable to parse it in a way to display it inside the expandable listview.
JSON data I recieve is
{
"success": 1,
"Message": "User exist",
"category": [{
"major_item_id": "1",
"Major_item_name": "Cement"
}, {
"major_item_id": "15",
"Major_item_name": "Wire"
}],
"subcategory": [{
"major_item_id": "1",
"name_item":"abc1"
}, {
"major_item_id": "1",
"name_item":"pqrs2"
}, {
"major_item_id": "15",
"name_item":"lmn"
}, {
"major_item_id": "15",
"name_item":"xyz"
}]}
What I want to is an expandable listview in format :
Cement //listHeader
-abc1 // child items
-pqrs2 // child items
Wire
-lmn
-xyz
An JSON file consist of many components. Here is the table defining the components of an JSON file and their description
Array([)
In a JSON file , square bracket ([) represents a JSON array
Objects({)
In a JSON file, curly bracket ({) represents a JSON object
Key
A JSON object contains a key that is just a string. Pairs of key/value make up a JSON object
Value
Each key has a value that could be string , integer or double e.t.c
Each implementations are different according to the json format that you have been used. You can easily find too many examples on internet.
http://www.tutorialspoint.com/android/android_json_parser.htm
You can use Gson to convert the JSON to an object like this:
Gson gson = new Gson();
Type listType = new TypeToken<YourObject>() {}.getType();
YourObject obj = gson.fromJson(getCurrentChat(), listType);
Once you have the correct object you can use the corresponding values to update your ExpandableListView.
I am parsing a JSON in my activity to display both text and images in a gridView.However i would like to try to add search functionality on this JSON and display the results on the activity.
For example consider a JSON script on an online server like this:
[
{
"title": "Ongata Rongai",
"image": "http://snapt.t15.org/melanie/real_estates/images/house1.jpg",
"rating": 5 500 000,
"releaseYear": 2014,
"genre": ["Spacious open lounge and kitchen"]
},
{
"title": "Kisumu",
"image": "http://snapt.t15.org/melanie/real_estates/images/house2.jpg",
"rating": 1 700 000,
"releaseYear": 2014,
"genre": ["high end townhouse."]
},
{
"title": "Mombasa",
"image": "http://snapt.t15.org/melanie/real_estates/images/house3.jpg",
"rating": 68 000,
"releaseYear": 2014,
"genre": ["Its fully fitted kitchedn pantry with a beautyful backyard."]
}
]
I would like to implement search of title like Kisumu and the result to be displayed will be from this part only so that i parse this alone:
[
{
"title": "Kisumu",
"image": "http://snapt.t15.org/melanie/real_estates/images/house2.jpg",
"rating": 1 700 000,
"releaseYear": 2014,
"genre": ["high end townhouse."]
}
]
Is there a way of achieving the above?
I am open to ideas and suggestions and any help will be appreciated.
You can't search json directly. Convert the json to List Java classes. Then use Java API like Predicate to find necessary java class that satisfy your condition. For eg: Title as Kusumu as you asked.
Which will limit the list of classes to one that satisfy the condition. Then convert the java class back to json if you need Json.
Note: You can use GSON for easy class <-> json conversion
UPDATE
For converting Json to Java classes
List<ModelClass> models = new Gson().fromJson(yourJsonString, ModelClass.java);
Then using predicate
public static Predicate<ModelClass> isHavingTitle() {
return p -> p.getTitle().contains("Kusumu");
}
In Java 8 predicate filtering can be done like below in other case you can just google it. This is a sample
public static List<ModelClass> filterEmployees (List< ModelClass > models, Predicate< ModelClass > predicate) {
return models.stream().filter( predicate ).collect(Collectors.<ModelClass>toList());
}
I didn't compile any of this :)
String filteredJson = new Gson().toJson(models);
this method give resualt of server and title as you want and returns search result as JSONObject
private JSONObject SearchMethod(String result,String Key)
{
//result is your server response
//Key is Kisumu
JSONArray json = new JSONArray(result);
for(int i=0;i<json.length();i++){
JSONObject e = json.getJSONObject(i);
if(e.c.getString("title").contains(Key))
return e ;
}
return null;
}
I found a lot of tutorials here, how to parse JSON Data of an JSON Array.
But my JSON File is a little bit complicate (for me). It has the following structure:
JSON File (excerpt)
{
"data": {
"schedule_id": {
"12": {
"name": "CP",
"d_id": [
"7"
]
},
"17": {
"name": "WT",
"d_id": [
"88",
"14"
]
}
}
}
}
Java Code (excerpt)
Info: I've parsed the json into "json" using HTTP GET in another Activity.
JSONObject dataJsonData = json.getJSONObject("data").getJSONObject("schedule_id");
Now I would parse through the ids using a "for"-loop:
ArrayList<String> parsedNameList = new ArrayList<String>();
for (int i = 0; i < idontknow; i++) {
String s = new Integer(i).toString();
parsedNameList.add(dateJsonData.getJSONObject(i).getString("name"));
}
This would add each value of "name" to the ArrayList.
But there are 2 problems:
1. The "schedule_id"s are messed up and incomplete. For example, there is no id "0" and, like in given json, the ids "13, 14, 15, 16" are missing.
2. The "schedule_id"s will be changed every day and will be mixed.
So I don't think, that I can use the predefined integer "i" because some integers aren't a "schedule_id". I could use this loop and would ignore empty entries in the ArrayList, but the JSON contains more than 200 ids - I think it would be more efficient, if there is another way to parse through this json.
I found some informations of the getJSONArray method, but the "d_id"s are Arrays - not the "schedule_ids".
Does anyone has an idea? Is there maybe a placeholder for the parameter of the getString method?
PS: Excuse my english, I'm from germany :)
I think this should work
Iterator keys = dataJsonData.keys();
while(keys.hasNext()) {
// loop to get the dynamic key
String currentDynamicKey = (String)keys.next();
// get the value of the dynamic key
String currentDynamicValue = dataJsonData .getString(currentDynamicKey);
parsedJsonList.add(currentDynamicValue );
}
Source: How to parse a dynamic JSON key in a Nested JSON result?
According to your context, it is better to change the json structure,if you have access to web service.
Request for json structure to be like this,
{
"data":{
"schedule":[
{
"id":12,
"name":"CP",
"d_id":[
"7"
]
},
{
"id":12,
"name":"CP",
"d_id":[
"7",
"88"
]
},
{
"id":200,
"name":"AT",
"d_id":[
"7",
"88"
]
}
]
}
}
Otherwise too much iteration can slow down you CPU.
I am having a few problems getting Facebook data to parse properly.
I am working on implementing part of an application to allow a user to select and use one of their own facebook photos within the app. I have gotten the facebook login/logout code working and I am currently getting the token (once logged in) in order to gather the users Album information. The permission set is also working nicely, however I am now stuck at trying to get the JSON information to parse correctly. Here is a sample snippet of the information I need to parse:
"data": [
{
"id": "3486732467234",
"from": {
"name": "Persons Name",
"id": "Persons ID"
},
"name": "Vacation",
"location": "City",
"link": "https://www.facebook.com/album.php?fbid=434235&id=324343&aid=2430",
"cover_photo": "3489234432",
"privacy": "everyone",
"count": 60,
"type": "normal",
"created_time": "2007-06-03T23:01:16+0000",
"updated_time": "2011-03-18T19:46:43+0000",
"can_upload": true
},
{
"id": "4043544665",
"from": {
"name": "Persons Name",
"id": "Persons ID"
},
"name": "Vacation 2",
"location": "City",
"link": "https://www.facebook.com/album.php?fbid=4043434665&id=508154335&aid=2555",
"cover_photo": "5434543",
"privacy": "everyone",
"count": 60,
"type": "normal",
"created_time": "2007-06-03T22:53:03+0000",
"updated_time": "2011-03-18T19:45:55+0000",
"can_upload": true
}],
...more paging JSON information ...}}
I need to be able to pull the album information in order to display the photo album names in a list (and use the ID in another query). Once I can get the albums to show, I would then use those IDs to perform another query to get the photos from that album. Again I can also get that information back, I am just not sure how to properly parse it.
Any useful tips that someone might have in being able to parse this into a listview would be greatly appreciated.
I need to target pulling the first ID and the Album name ("id": "3486732467234" and "name": "Vacation" from the first listing)
why don't you try with the json classes...
something like this:
jsonObject = new JSONObject(your_data);
jArray = jsonObject.getJSONArray("data");
for(int i =0;i<jArray.length();i++){
String name= jArray.getJSONObject(i).getString("name");
String location= jArray.getJSONObject(i).getString("location");
etc.
The new Android Facebook SDK 3.0 returns a Response object in the Response.Callback() listener when making Graph API calls. This response object can be used to create a GraphObject which can be used to get a JSON...ex:
Request graphRequest = Request.newGraphPathRequest(session, graphRequestString, new Request.Callback() {
#Override
public void onCompleted(Response response) {
//Create the GraphObject from the response
GraphObject responseGraphObject = response.getGraphObject();
//Create the JSON object
JSONObject json = responseGraphObject.getInnerJSONObject();
}
});