How to parse the following JSON - java

Here I am getting following JSON from local asset folder. All the JSON object is different in their name. Line 1, 3, 5, 7 .... upto 1000. Herewith I have attached the json for your reference.
{
"status": {
"rcode": 200,
"message": "OK"
},
"data": {
"0": {
"OutletName": "Test 1 ",
"Latitude": "16.123234",
"Longitude": "79.546745"
},
"1": {
"OutletName": "Test 2",
"Latitude": "16.343234",
"Longitude": "79.786745"
},
"2": {
"OutletName": "Test 3",
"Latitude": "19.1294",
"Longitude": "72.836122"
},
"3": {
"OutletName": "Test 4",
"Latitude": "19.136383",
"Longitude": "72.827997"
},
"6": {
"OutletName": "Test 5",
"Latitude": "19.136715",
"Longitude": "72.829248"
},
"7": {
"OutletName": "Test 6",
"Latitude": "19.128483",
"Longitude": "72.821199"
},
"8": {
"OutletName": "Test 7",
"Latitude": "19.128528",
"Longitude": "72.819388"
},
"10": {
"OutletName": "Test 8",
"Latitude": "19.140333",
"Longitude": "72.831095"
},
"11": {
"OutletName": "Test 9",
"Latitude": "19.14027",
"Longitude": "72.826285"
}
}
}
Here the Object name is different for all. So I try like this
private void parseJson() {
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONObject jObj = obj.getJSONObject("data");
System.out.println("jObj.length()==> " + jObj.length());
for (int i = 0; i < jObj.length(); i++) {
if (jObj.has(String.valueOf(i)) && !jObj.isNull(String.valueOf(i))) {
JSONObject jObj1 = jObj.getJSONObject(String.valueOf(i));
System.out.println("Index==> "+i);
System.out.println("OutletName==> "+jObj1.getString("OutletName"));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
But here I not getting all the data. Please help me to parse this kind of JSON.
Thanks in advance.

You need to use the keys method like this:
private void parseJson() {
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONObject jObj = obj.getJSONObject("data");
Iterator iter = jObj.keys()
while(iter.hasNext()) {
String key = (String) iter.next();
JSONObject jObj1 = jObj.getJSONObject(key);
System.out.println("Index==> "+ key);
System.out.println("OutletName==> "+jObj1.getString("OutletName"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Edit:
fixed some type conversion

The value of "data" in your JSON is not an array, hence you cannot iterate over it directly. You will need to first convert it into a HashMap and then iterate over the keyset. You can use Jackson library to convert JSONObject to HashMap. You can also refer to this answer for conversion to map.

first of all you will have to do changes in your json response
{
"status": {
"rcode": 200,
"message": "OK"
},
"data": { //this has to be array in your json
"0": { // and these are object inside the arrays
"OutletName": "Test 1 ",
"Latitude": "16.123234",
"Longitude": "79.546745"
},
"1": { // and these are object inside the arrays
"OutletName": "Test 2",
"Latitude": "16.343234",
"Longitude": "79.786745"
},
"2": {
"OutletName": "Test 3",
"Latitude": "19.1294",
"Longitude": "72.836122"
},
"3": {
"OutletName": "Test 4",
"Latitude": "19.136383",
"Longitude": "72.827997"
},
"6": {
"OutletName": "Test 5",
"Latitude": "19.136715",
"Longitude": "72.829248"
},
"7": {
"OutletName": "Test 6",
"Latitude": "19.128483",
"Longitude": "72.821199"
},
"8": {
"OutletName": "Test 7",
"Latitude": "19.128528",
"Longitude": "72.819388"
},
"10": {
"OutletName": "Test 8",
"Latitude": "19.140333",
"Longitude": "72.831095"
},
"11": {
"OutletName": "Test 9",
"Latitude": "19.14027",
"Longitude": "72.826285"
}
}
}
and dont over json object for its size to access its object it has to be JSONArray
get the array data in JSONArray like this
JSONArray dataArray = response_obj.getJSONArray("data");
and then iterate like this
for (int i = 0; i < dataArray.length(); i++)
{
//write your code here to access all the object as for loop executes on by one
}

Related

how to parse the json response which starts with an array

response:
[
{
"id": "e9299032e8a34d168def176af7d62da3",
"createdAt": "Nov 8, 2017 9:46:40 AM",
"model": {
"id": "eeed0b6733a644cea07cf4c60f87ebb7",
"name": "color",
"app_id": "main",
"created_at": "May 11, 2016 11:35:45 PM",
"model_version": {}
},
"input": {
"id": "df6eae07cd86483f811c5a2202e782eb",
"data": {
"concepts": [],
"metadata": {},
"image": {
"url": "http://www.sachinmittal.com/wp-content/uploads/2017/04/47559184-image.jpg"
}
}
},
"data": [
{
"hex": "#f59b2d",
"webSafeHex": "#ffa500",
"webSafeColorName": "Orange",
"value": 0.0605
},
{
"hex": "#3f1303",
"webSafeHex": "#000000",
"webSafeColorName": "Black",
"value": 0.2085
},
{
"hex": "#a33303",
"webSafeHex": "#8b0000",
"webSafeColorName": "DarkRed",
"value": 0.3815
},
{
"hex": "#000000",
"webSafeHex": "#000000",
"webSafeColorName": "Black",
"value": 0.34275
},
{
"hex": "#f7ce93",
"webSafeHex": "#ffdead",
"webSafeColorName": "NavajoWhite",
"value": 0.00675
}
],
"status": {}
}
]
need to parse this reponse in json. Please help me out.
You can try something like this..
try{
JSONArray array= new JSONArray(Yourresponse);
for(int i=0; i<=array.length();i++){
JSONObject jsonObject=array.getJSONObject(i);
String id= jsonObject.getString("id");
String created_at= jsonObject.getString("createdAt");
String model_id = jsonObject.getJSONObject("model").getString("id");
String app_id=jsonObject.getJSONObject("model").getString("app_id");
//So On... Depends on your requirements. It's just an idea!
}
}
catch (Exception e){
e.printStackTrace();
}

Unable to extract out name in 4 level deep nested JSON array

I am trying to create android app that shows details about the restaurants. I have parseJSON function which takes string json as an argument. What I am trying to do for now is to show name of individual restaurants in the json below. I am able to extract individual restaurant(3 levels deep) and print them out through log.d console but I am not able to extract out the individual restaurant's name. Here is the code
public void parseJSON(String jsonQuery){
//Parsing JSON
try {
JSONObject jsonObject = new JSONObject(jsonQuery);
if (jsonObject != null){
JSONArray restaurants = jsonObject.getJSONArray("restaurants");
for (int i = 0; i < restaurants.length(); i++){
JSONObject restaurant = restaurants.getJSONObject(i);
if (restaurant != null){
String tempName = restaurant.getString("name");
Log.d(TAG, "Restaurant name: " + tempName);
}
}
}
} catch (JSONException e) {
Log.d(TAG, "Exception catched: " + e);
e.printStackTrace();
}
}
Here is my json file.
{
"results_found": 1281966,
"results_start": 0,
"results_shown": 2,
"restaurants": [
{
"restaurant": {
"R": {
"res_id": 16607974
},
"apikey": "123456789",
"id": "16607974",
"name": "Bassine Specialty Cheese",
"url": "https://www.zomato.com/bass-vic/bassine-specialty-cheese-bass?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
"location": {
"address": "2125 Bass Hwy",
"locality": "Bass",
"city": "Bass",
"city_id": 1341,
"latitude": "-38.4833750000",
"longitude": "145.4670320000",
"zipcode": "3995",
"country_id": 14,
"locality_verbose": "Bass, Bass"
},
"switch_to_order_menu": 0,
"cuisines": "Ice Cream",
"average_cost_for_two": 7,
"price_range": 1,
"currency": "$",
"offers": [],
"thumb": "",
"user_rating": {
"aggregate_rating": "2.9",
"rating_text": "Average",
"rating_color": "FFBA00",
"votes": "5"
},
"photos_url": "https://www.zomato.com/bass-vic/bassine-specialty-cheese-bass/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop",
"menu_url": "https://www.zomato.com/bass-vic/bassine-specialty-cheese-bass/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop",
"featured_image": "",
"has_online_delivery": 0,
"is_delivering_now": 0,
"deeplink": "zomato://restaurant/16607974",
"has_table_booking": 0,
"events_url": "https://www.zomato.com/bass-vic/bassine-specialty-cheese-bass/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
"establishment_types": []
}
},
{
"restaurant": {
"R": {
"res_id": 17649424
},
"apikey": "3d93604b4a84d85f374f39ea3b644132",
"id": "17649424",
"name": "Schobels' Restaurant",
"url": "https://www.zomato.com/columbus-tx/schobels-restaurant-columbus?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
"location": {
"address": "2020 Milam St 78934",
"locality": "Columbus",
"city": "Columbus",
"city_id": 9241,
"latitude": "29.6965000000",
"longitude": "-96.5405000000",
"zipcode": "78934",
"country_id": 216,
"locality_verbose": "Columbus, Columbus"
},
"switch_to_order_menu": 0,
"cuisines": "German, Southern",
"average_cost_for_two": 25,
"price_range": 2,
"currency": "$",
"offers": [],
"thumb": "",
"user_rating": {
"aggregate_rating": "4.0",
"rating_text": "Very Good",
"rating_color": "5BA829",
"votes": "164"
},
"photos_url": "https://www.zomato.com/columbus-tx/schobels-restaurant-columbus/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop",
"menu_url": "https://www.zomato.com/columbus-tx/schobels-restaurant-columbus/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop",
"featured_image": "",
"has_online_delivery": 0,
"is_delivering_now": 0,
"deeplink": "zomato://restaurant/17649424",
"has_table_booking": 0,
"events_url": "https://www.zomato.com/columbus-tx/schobels-restaurant-columbus/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
"establishment_types": []
}
}
]
}
Error which I am getting:
org.json.JSONException: No value for name
Your jsonQuery contain JsonObject inside JsonObject named restaurant so you need to get JsonObject from JSONObject restaurant = restaurants.getJSONObject(i);
Like JSONObject restaurantsJSONObject =restaurant.getJSONObject("restaurant");
Try below example to get name from object
try {
JSONObject jsonObject = new JSONObject(jsonQuery);
if (jsonObject != null){
JSONArray restaurants = jsonObject.getJSONArray("restaurants");
for (int i = 0; i < restaurants.length(); i++){
JSONObject restaurant = restaurants.getJSONObject(i);
JSONObject restaurantsJSONObject =restaurant.getJSONObject("restaurant");
if(restaurantsJSONObject.has("name")) {
String tempName = restaurantsJSONObject.getString("name");
Log.d(TAG, "Restaurant name: " + tempName);
}
}
}
} catch (JSONException e) {
Log.d(TAG, "Exception catched: " + e);
e.printStackTrace();
}

JSON decode in android

How can I retrieve value from this JSON in android?
I want to create an ArrayList. I've tried JSONObject to access the value. But It need to create object for every city. I want to access all city using a loop inside it like as follows.
{
"City A": {
"Position": {
"Longitude": 9.96233,
"Latitude": 49.80404
}
},
"City B": {
"Position": {
"Longitude": 6.11499,
"Latitude": 50.76891
}
},
"City C": {
"Position": {
"Longitude": 6.80592,
"Latitude": 51.53548
}
},
"City D": {
"Position": {
"Longitude": 9.50523,
"Latitude": 51.31991
}
},
"City E": {
"Position": {
"Longitude": 9.66089,
"Latitude": 48.70158
}
},
"City F": {
"Position": {
"Longitude": 9.93368,
"Latitude": 53.55608
}
},
"City G": {
"Position": {
"Longitude": 11.56122,
"Latitude": 48.14496
}
},
"City H": {
"Position": {
"Longitude": 13.34253,
"Latitude": 52.5319
}
},
"City I": {
"Position": {
"Longitude": 6.11327,
"Latitude": 50.77715
}
},
"City J": {
"Position": {
"Longitude": 13.36671,
"Latitude": 52.54344
}
}
}
How can I manage this when I want the same?
You can do this:
City.java (model class)
public class City implements Comparable {
private String name; // The name of the city
private double latitude; // its latitude
private double longitude; // its longitude
public City(String name, double latitude, double longitude) {
this.name = name;
this.latitude = latitude;
this.longitude = longitude;
}
#Override
public String toString() {
return name + ": " + latitude + " - " + longitude;
}
#Override
public int compareTo(Object c) {
City city = (City) c;
return name.compareTo(city.name);
}
}
App.java:
public class App {
public static void main(String[] args) {
// Your JSON string
String jsonString = "{\"City A\":{\"Position\":{\"Longitude\":9.96233,\"Latitude\":49.80404}},\"City B\":{\"Position\":{\"Longitude\":6.11499,\"Latitude\":50.76891}},\"City C\":{\"Position\":{\"Longitude\":6.80592,\"Latitude\":51.53548}},\"City D\":{\"Position\":{\"Longitude\":9.50523,\"Latitude\":51.31991}},\"City E\":{\"Position\":{\"Longitude\":9.66089,\"Latitude\":48.70158}},\"City F\":{\"Position\":{\"Longitude\":9.93368,\"Latitude\":53.55608}},\"City G\":{\"Position\":{\"Longitude\":11.56122,\"Latitude\":48.14496}},\"City H\":{\"Position\":{\"Longitude\":13.34253,\"Latitude\":52.5319}},\"City I\":{\"Position\":{\"Longitude\":6.11327,\"Latitude\":50.77715}},\"City J\":{\"Position\":{\"Longitude\":13.36671,\"Latitude\":52.54344}}}";
JSONObject jsonObj = new JSONObject(jsonString);
List<City> cities = new ArrayList<>(); // The arraylist that will hold the cities
// A JSON array with the the names of all the cities
JSONArray cityNames = jsonObj.names();
for(int i = 0; i < jsonObj.length(); i++) {
String cityName = cityNames.getString(i);
JSONObject jsonCity = jsonObj.getJSONObject(cityName);
JSONObject jsonPosition = jsonCity.getJSONObject("Position");
cities.add(new City(cityName, jsonPosition.getDouble("Latitude"), jsonPosition.getDouble("Longitude")));
}
Collections.sort(cities); // Sort the cities' arraylist by city name
cities.forEach(System.out::println); // Print the cities
}
}
Output:
City A: 49.80404 - 9.96233
City B: 50.76891 - 6.11499
City C: 51.53548 - 6.80592
City D: 51.31991 - 9.50523
City E: 48.70158 - 9.66089
City F: 53.55608 - 9.93368
City G: 48.14496 - 11.56122
City H: 52.5319 - 13.34253
City I: 50.77715 - 6.11327
City J: 52.54344 - 13.36671
You can use Google gson to convert string to Java object.
TO get Data with for loop you should have JSONArray.
This is response you should use.
Then using this code you can apply for loop and List or ArrayList to save data.
[{
"city_name": "City A",
"Position": {
"Longitude": 9.96233,
"Latitude": 49.80404
}
}, {
"city_name": "City B",
"Position": {
"Longitude": 6.11499,
"Latitude": 50.76891
}
}, {
"city_name": "City C",
"Position": {
"Longitude": 6.80592,
"Latitude": 51.53548
}
}, {
"city_name": "City D",
"Position": {
"Longitude": 9.50523,
"Latitude": 51.31991
}
}, {
"city_name": "City E",
"Position": {
"Longitude": 9.66089,
"Latitude": 48.70158
}
}, {
"city_name": "City F",
"Position": {
"Longitude": 9.93368,
"Latitude": 53.55608
}
}, {
"city_name": "City G",
"Position": {
"Longitude": 11.56122,
"Latitude": 48.14496
}
}, {
"city_name": "City H",
"Position": {
"Longitude": 13.34253,
"Latitude": 52.5319
}
}, {
"city_name": "City I",
"Position": {
"Longitude": 6.11327,
"Latitude": 50.77715
}
}, {
"city_name": "City J",
"Position": {
"Longitude": 13.36671,
"Latitude": 52.54344
}
}]
then Java code as:
JSONArray json_array= new JSONArray(responseString);
for(int i= 0; i<json_array.length(); i++){
JSONObject json_object = json_array.get(i);
String cityname=json_object.getString("city_name");
JSONObject json_object_position = json_object.getJSONObject("Position");
long longitude=json_object_position.getString("Longitude");
long latitude=json_object_position.getString("Latitude");
// Save the data in List you want.
}
You can use names() method of JSONObject.
Try this :
JSONObject jsonObject = new JSONObject(yourjsonString);
JSONArray jarray = jsonObject.names();
for(int i= 0; i<jarray.length(); i++){
String name = jarray.getString(i);
}

How get array value from nested json data (java android)

I've got the title, author, and published date from json data to my android app.
I'm trying to get event_start_date and event_location, but failed. can anyone help me how I can get it?
Json:
{
"status": "ok",
"count": 1,
"count_total": 29,
"pages": 29,
"posts": [
{
"id": 2815,
"type": "event",
"slug": "itb-integrated-career-days",
"url": "http://example.com/event/itb-integrated-career-days/",
"status": "publish",
"title": "Title",
"title_plain": "Title",
"content": "<p>test</p>\n",
"excerpt": "<p>test […]</p>\n",
"date": "2015-09-25 01:09:40",
"modified": "2015-09-29 22:52:35",
"categories": [],
"tags": [],
"author": {
"id": 1,
"slug": "john",
"name": "john",
"first_name": "",
"last_name": "",
"nickname": "john",
"url": "",
"description": ""
},
"comments": [],
"attachments": [
{
"id": 2817,
"url": "http://example.com/wp-content/uploads/2015/09/bannertkt102015.gif",
"slug": "bannertkt102015",
"title": "bannertkt102015",
"description": "",
"caption": "",
"parent": 2815,
"mime_type": "image/gif",
"images": {
"full": {
"url": "http://example.com/wp-content/uploads/2015/09/bannertkt102015.gif",
"width": 1000,
"height": 563
},
"thumbnail": {
"url": "http://example.com/wp-content/uploads/2015/09/bannertkt102015-150x150.gif",
"width": 150,
"height": 150
},
"medium": {
"url": "http://example.com/wp-content/uploads/2015/09/bannertkt102015-300x169.gif",
"width": 300,
"height": 169
},
"large": {
"url": "http://example.com/wp-content/uploads/2015/09/bannertkt102015.gif",
"width": 1000,
"height": 563
},
"blog-default": {
"url": "http://example.com/wp-content/uploads/2015/09/bannertkt102015-806x300.gif",
"width": 806,
"height": 300
}
}
},
{
"id": 2818,
"url": "http://example.com/wp-content/uploads/2015/09/2015-09-25_012008.jpg",
"slug": "2015-09-25_012008",
"title": "2015-09-25_012008",
"description": "",
"caption": "",
"parent": 2815,
"mime_type": "image/jpeg",
"images": {
"full": {
"url": "http://example.com/wp-content/uploads/2015/09/2015-09-25_012008.jpg",
"width": 589,
"height": 529
},
"thumbnail": {
"url": "http://example.com/wp-content/uploads/2015/09/2015-09-25_012008-150x150.jpg",
"width": 150,
"height": 150
},
"medium": {
"url": "http://example.com/wp-content/uploads/2015/09/2015-09-25_012008-300x269.jpg",
"width": 300,
"height": 269
},
"large": {
"url": "http://example.com/wp-content/uploads/2015/09/2015-09-25_012008.jpg",
"width": 589,
"height": 529
},
"blog-default": {
"url": "http://example.com/wp-content/uploads/2015/09/2015-09-25_012008-589x300.jpg",
"width": 589,
"height": 300
}
}
}
],
"comment_count": 0,
"comment_status": "open",
"thumbnail": "http://example.com/wp-content/uploads/2015/09/2015-09-25_012008-150x150.jpg",
"custom_fields": {
"event_location": [
"New York"
],
"event_start_date": [
"10/23/2015"
],
"event_start_time": [
"09:00 AM"
],
"event_start_date_number": [
"1445590800"
],
"event_address_country": [
"US"
],
"event_address_state": [
"Jawdst"
],
"event_address_city": [
"New York"
],
"event_address_address": [
"Gedung Sasana Budaya Ganesha (Sabuga) Jalan Tamansari 73, New York"
],
"event_address_zip": [
"42132"
],
"event_phone": [
"5345"
],
"post_views_count": [
"23"
]
}
}
]
}
And my function
public void parseJson(JSONObject json) {
try {
info.pages = json.getInt("pages");
// parsing json object
if (json.getString("status").equalsIgnoreCase("ok")) {
JSONArray posts = json.getJSONArray("posts");
info.feedList = new ArrayList<PostItem>();
for (int i = 0; i < posts.length(); i++) {
Log.v("INFO",
"Step 3: item " + (i + 1) + " of " + posts.length());
try {
JSONObject post = (JSONObject) posts.getJSONObject(i);
PostItem item = new PostItem();
item.setTitle(Html.fromHtml(post.getString("title"))
.toString());
item.setDate(post.getString("date"));
item.setId(post.getInt("id"));
item.setUrl(post.getString("url"));
item.setContent(post.getString("content"));
if (post.has("author")) {
Object author = post.get("author");
if (author instanceof JSONArray
&& ((JSONArray) author).length() > 0) {
author = ((JSONArray) author).getJSONObject(0);
}
if (author instanceof JSONObject
&& ((JSONObject) author).has("name")) {
item.setAuthor(((JSONObject) author)
.getString("name"));
}
}
if (post.has("tags") && post.getJSONArray("tags").length() > 0) {
item.setTag(((JSONObject) post.getJSONArray("tags").get(0)).getString("slug"));
}
// TODO do we dear to remove catch clause?
try {
boolean thumbnailfound = false;
if (post.has("thumbnail")) {
String thumbnail = post.getString("thumbnail");
if (thumbnail != "") {
item.setThumbnailUrl(thumbnail);
thumbnailfound = true;
}
}
if (post.has("attachments")) {
JSONArray attachments = post
.getJSONArray("attachments");
// checking how many attachments post has and
// grabbing the first one
if (attachments.length() > 0) {
JSONObject attachment = attachments
.getJSONObject(0);
item.setAttachmentUrl(attachment
.getString("url"));
// if we do not have a thumbnail yet, get
// one now
if (attachment.has("images")
&& !thumbnailfound) {
JSONObject thumbnail;
if (attachment.getJSONObject("images")
.has("post-thumbnail")) {
thumbnail = attachment
.getJSONObject("images")
.getJSONObject(
"post-thumbnail");
item.setThumbnailUrl(thumbnail
.getString("url"));
} else if (attachment.getJSONObject(
"images").has("thumbnail")) {
thumbnail = attachment
.getJSONObject("images")
.getJSONObject("thumbnail");
item.setThumbnailUrl(thumbnail
.getString("url"));
}
}
}
}
} catch (Exception e) {
Log.v("INFO",
"Item "
+ i
+ " of "
+ posts.length()
+ " will have no thumbnail or image because of exception!");
e.printStackTrace();
}
if (item.getId() != info.ignoreId)
info.feedList.add(item);
} catch (Exception e) {
Log.v("INFO", "Item " + i + " of " + posts.length()
+ " has been skipped due to exception!");
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
This should do it
JSONObject custom_fields = post.getJSONObject("custom_fields");
JSONArray event_start_date_array = custom_fields.getJSONArray("event_start_date");
JSONArray event_location_array = custom_fields.getJSONArray("event_location");
String event_start_date = event_start_date_array.getString(0);
String event_location = event_location_array.getString(0);

JSONObject keys order reverse

I searched across many sources but could not find the answer to this one..
Its a know fact that a JSONObject keys are returned in reverse order.
Is there any way to recurse through a JSONObject in the correct order as appearing in JSON
String json;
JSONObject obj = new JSONObject(json)
Iterator<String> keys = json.keys() ---> order is reversed
I understand JSONObject is unordered, perhaps there is a way to order it????
JSON is of type below..and the keys start getting recursed from the bottom most styleHint tag
"sections": {
"1": {
"1": {
"1": {
"title": "xxx",
"text": "xxx",
"tags": {
"audience": {
"1": {
"name": "xxx",
"title": "xxx",
"id": "xxx"
}
},
"styleHint": {
"1": {
"name": "xxx",
"title": "xxx",
"id": "xxx"
}
}
}
},
"title": "xxx",
"text": "xxx",
"tags": {
"audience": {
"1": {
"name": "xxx",
"title": "xxx",
"id": "xxx"
}
},
"styleHint": {
"1": {
"name": "xxx",
"title": "xxx",
"id": "xxx"
}
}
}
},
"2": {
"title": "xxx",
"text": "xxx",
"tags": {
"audience": {
"1": {
"name": "xxx",
"title": "xxx",
"id": "xxx"
}
},
"styleHint": {
"1": {
"name": "xxx",
"title": "xxx",
"id": "xxx"
}
}
}
},
"title": "xxx",
"text": "xxx",
"tags": {
"audience": {
"1": {
"name": "xxx",
"title": "xxx",
"id": "xxx"
},
"2": {
"name": "xxx",
"title": "xxx",
"id": "xxx"
}
},
"styleHint": {
"1": {
"name": "xxx",
"title": "xxx",
"id": "xxx"
}
}
}
},
"2": {
"title": "xxx",
"text": "xxx",
"tags": {
"audience": {
"1": {
"name": "xxx",
"title": "xxx",
"id": "xxx"
}
},
"styleHint": {
"1": {
"name": "xxx",
"title": "xxx",
"id": "xxx"
}
}
},
"anchor":"xxx"
},
"3": {
"1": {
"title": "xxx",
"text": "xxx",
"tags": {
"audience": {
"tag": {
"name": "xxx",
"title": "xxx",
"id": "xxx"
}
},
"styleHint": {
"tag": {
"name": "xxx",
"title": "xxx",
"id": "xxx"
}
}
}
},
"title": "xxx",
"text": "xxx",
"tags": {
"audience": {
"1": {
"name": "xxx",
"title": "xxx",
"id": "xxxx"
}
},
"styleHint": {
"1": {
"name": "xx",
"title": "xxx",
"id": "xxxx"
}
}
}
}
}
How about that
Get your org.json.JSONObject source and change these
public ListIterator keys() {
ListIterator iter = new ArrayList(this.keySet()).listIterator();
return iter;
}
and
/**
* Construct an empty JSONObject.
*/
public JSONObject() {
this.map = new LinkedHashMap();
}
and
/**
* The map where the JSONObject's properties are kept.
*/
private final LinkedHashMap map;
and
public JSONObject(Map map) {
this.map = new LinkedHashMap();
if (map != null) {
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
Map.Entry e = (Map.Entry) i.next();
Object value = e.getValue();
if (value != null) {
this.map.put(e.getKey(), wrap(value));
}
}
}
}
I hope I don't burn in hell for that.

Categories

Resources