E
E
emigrantdd2014-10-17 17:19:57
Java
emigrantdd, 2014-10-17 17:19:57

How to solve json problem?

Good day. In general, the problem is that when I add a new class to the ArrayList , to which I pass the variables in one order, it returns them to me in another.
Here is the code with which I pass variables from the main class (now, for a better understanding, instead of variables, I will simply pass a string):

temp_list_gps_wifi.add(new temp_class_gps_wifi("1","2","3"));

temp_list_gps_wifi - as you understand it is an ArrayList , and temp_class_gps_wifi is a class, respectively.
When returning from the temp_class_gps_wifi class , the order in the string is :{"1","3","2" }.
Here is the temp_class_gps_wifi code:
package dd_pc.service;

/**
 * Created by dd on 17.10.2014.
 */
public class temp_class_gps_wifi {
    String gps;String wifi;
    String wifi_mac;
    public temp_class_gps_wifi (String gps,String wifi,
            String wifi_mac ){
        this.gps=gps;  this.wifi_mac=wifi_mac;
        this.wifi=wifi;

    }

    public String getGps() {
        return gps;
    }
    public String getWifi() {
        return wifi;
    }
    public String getWifi_mac() {
        return wifi_mac;
    }
    public void setGps(String gps) {
        this.gps = gps;
    }

    public void setWifi_mac(String wifi_mac) {
        this.wifi_mac = wifi_mac;
    }

    public void setWifi(String wifi) {
        this.wifi = wifi;
    }



}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tyranron, 2014-10-17
@emigrantdd

ArrayList here, most likely, absolutely nothing to do with it, the matter is in the order of serialization of class fields.
Here is what I found here :
Another point that many do not know. Standard serialization respects the order in which the fields in the class are declared. In any case, this was the case in earlier versions, in the JVM version 1.6 of the Oracle implementation, the order is already unimportant, the field type and name are important. The composition of the methods is very likely to affect the standard mechanism, despite the fact that the fields may generally remain the same.
That is, it is logical to assume that when serializing a class, it is absolutely not guaranteed to preserve either the order in which fields are declared in the class, and certainly not the order in which parameters are passed to the class constructor.
PS And what for to you in JSON' it is guaranteed to keep the order of fields? Maybe it's not so critical in reality?

V
Vladimir Yakushev, 2014-10-18
@VYakushev

You write about JSON, but where is the code for adding / withdrawing to JSON? Judging by your line {"1","3","2" }, I'm pretty sure you're using JSON incorrectly. If you want to pass an object, then pass JSONObject in the form

{"gps_wifi":{"gps":"1", "wifi_mac":"3", "wifi":"2" }}
. Then the order of the values ​​won't matter. In general, it is convenient to use Gson for these purposes . Then adding your object will look like:
, and the withdrawal:
Or you can add two methods to your class: toJson() and fromJson(), and write the necessary code manually.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question