0

Hi i have a php script that input user data into mysql and from mysql i get the results in an array and finally to json format by json_encode functino of PHP. The contents of the file are provided below.

[{"priority":"1","rule_body":"person(?x),pid(?y),haspid(?x,?y)","cons":"patient(?x)","boolean":"1"},
{"priority":"1","rule_body":"patient(?x),hasbp(high)","cons":"situation(emergency)","boolean":"1"},
{"priority":"1","rule_body":"patient(?x),hasbp(high)","cons":"situation(emergency)","boolean":"1"},
{"priority":"1","rule_body":"patient(?x),hasbp(high)","cons":"situation(emergency)","boolean":"1"},
{"priority":"1","rule_body":"aaa(bbb)","cons":"z(x)","boolean":"1"},
{"priority":"1","rule_body":"aaa(bbb)","cons":"z(x)","boolean":"1"},
{"priority":"1","rule_body":"aaa(bbb)","cons":"z(x)","boolean":"1"}]

However now i need to get the json read in the JAVA and for ease i copy and paste the contents of the json file to string. I am able to read the contents but the positions has changed to the following sequence

here is the output

    LENGTH IS____7
{"priority":"1","boolean":"1","rule_body":"person(?x),pid(?y),haspid(?x,?y)","cons":"patient(?x)"}
{"priority":"1","boolean":"1","rule_body":"patient(?x),hasbp(high)","cons":"situation(emergency)"}
{"priority":"1","boolean":"1","rule_body":"patient(?x),hasbp(high)","cons":"situation(emergency)"}
{"priority":"1","boolean":"1","rule_body":"patient(?x),hasbp(high)","cons":"situation(emergency)"}
{"priority":"1","boolean":"1","rule_body":"aaa(bbb)","cons":"z(x)"}
{"priority":"1","boolean":"1","rule_body":"aaa(bbb)","cons":"z(x)"}
{"priority":"1","boolean":"1","rule_body":"aaa(bbb)","cons":"z(x)"}

as you can see that the boolean has to be in the end but it pops up on the second number. i treid differenct codes from tutorials and did some my own as below but the result is same: help is needed in this matter.

jsonInput=[....as shown above the json contents....]
JSONArray ja = new JSONArray(jsonInput);
System.out.println("LENGTH IS____"+ja.length());
  for(int x=0;x<ja.length();x++)
  {
      JSONObject jb = ja.getJSONObject(x);
      System.out.println(jb);
  }
  System.out.println("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKK");
Shahensha Khan
  • 265
  • 1
  • 19

1 Answers1

2

JSON doesn't specify an order for keys As Adrian Smith said :

JSON libraries are free to rearrange the order of the elements as they see fit. This is not a bug.

You probably use org.json. You can use the getString("myKey") to access the value in your JSONObject without knowing his position.

An other solution is to create a class that represents your data:

public class myClass{
    private int priority;
    private String rule_body;
    private String cons;
    private String boolean; //STRING ! 1 is not a valid boolean value.
}

Then create an ArrayList of your class and deserialize your JSON in it with Gson.

Gson myGsonTool = new Gson();
ArrayList<myClass> myData = gson.fromJson(myJsonString, ArrayList<myClass>.class);

Then, you have a POJO with all your data. You can access easily to your data now:

//If i want the value of rule body:
myData.get(0).getRule_Body();

I do not know the purpose of your program, so this is maybe not the best solution.

Edit: Gson maintains the order of records.

Community
  • 1
  • 1
Clément Duveau
  • 399
  • 3
  • 14