0

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.

1
  • What property name from which element are you missing? The third element is 'UUID':'300V' and looks just like all the others, doesn't it? Commented Jan 22, 2011 at 20:41

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

5 Comments

or JSON.parse(); if it exists :)
From the third element onwards i.e 'UUID':'300V' it is like a list..where UUID and 300V both should be read.....here UUID is not the property name....can we read that...is it possible with this format...?? we cant say v.UUID..
@user576875 -- Hey thanks. I never used that. @user585900 -- I did not get you, your JSON says it's property. If it's an array. Please update your question so. Even if it's array. It can be read. By the way, the above is javaScript not Java as you requested. I have updated the question.
Hey Thank You.....wanted to confirm the same...BTW do u know how do we read it in java....using JSON Object or Array...
You will have to have this associated with some property. It can't just be lying around. like "specs":"'Version':'0.1.0'". I mean it's string representation of an object.. same as Java object. You can't just have something like class MyClass{ "'Version':'0.1.0'"} -- you must have something like class MyClass{ String specs = "'Version':'0.1.0'"}. Same holds true for JSON.
0

With which language do you want to do that ?

Here is the solution for PHP :

$data = json_decode('the json');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.