user587092
user587092

Reputation: 1

Reading JSON String using JSON/Gson

I have the JSON String of the below format which I get as a http request in Java. I need to get the name and values of the below JSON string, but I am not able to get the correct solution.

Can any one tell me how to parse this? Also, let me know if we will be able to format this string because there is no property names from the 3 element.

The string format is

{

 'appname':'application',
 'Version':'0.1.0',
 'UUID':'300V',
 'WWXY':'310W',
 'ABCD':'270B',
 'YUDE':'280T'

}

edit#1 formatted the question.

Upvotes: 0

Views: 3198

Answers (2)

Nishant
Nishant

Reputation: 55866

In JavaScript, you can do something like

 var v = eval("("+data_from_server+")");
 var aName = v.appname;

For example this script will alert appname.

    <script>
        var serverdata = "{'appname':'application', 'Version':'0.1.0', 'UUID':'300V', 'WWXY':'310W', 'ABCD':'270B', 'YUDE':'280T'}";
        var v = eval("("+serverdata+")");
        alert(v.appname);
    </script>

Based on your comment on this answer, here is a way to parse in Java

In Java, you may want to leverage GSon. See here.

You need to define a Java class that maps the JSON object one-to-one. Then ask GSon to create a Java object using the JSON String. Here is the example.

Your Java class that maps JSON should look like this

    public  class MyData{
        public String appname;
        public String Version;
        public String UUID;
        public String WWXY;
        public String ABCD;
        public String YUDE;
        public MyData(){}
    }

And you parse in Java like this.

    String jsons = "{'appname':'application', 'Version':'0.1.0', 'UUID':'300V', 'WWXY':'310W', 'ABCD':'270B', 'YUDE':'280T'}";
    Gson gson = new Gson();
    MyData obj = gson.fromJson(jsons, MyData.class);
    System.out.println("ada "+ obj.appname);

Upvotes: 3

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99921

With which language do you want to do that ?

Here is the solution for PHP :

$data = json_decode('the json');

Upvotes: 0

Related Questions