Solved

How do i rename a key in JSONObject

Posted on 2009-07-09
12
3,035 Views
Last Modified: 2013-11-08
i am trying to rename  "displayName" key to"text",

where key "dispalyname" is a part of JSONArray which is nested in JSONObject.

like:

{"jsonText":[

{"description":"Employee","displayName":"Guillaumin, Xavier","email":"xguillaumin@hersheys.com","location":"Carretera GDL-ElCastilloKM8.05","title":"Financial Coordinator Global Exports","userId":"HERXGG40"},

{"description":"Former Employee","displayName":"Lugo, Javier","email":"JLugo@Hersheys.com","location":"","title":"Special Projects Manager","userId":"HERJEL40"},

{"description":"Employee","displayName":"Plitnick, Xavier F","email":"HFXFP001@hersheys.com","location":"6 Scotch Pine Drive","title":"HZ_1526_Mst Wrap Mech","userId":"HFXFP001"}

]}


Can anyone suggest way to rename ?
JSONArray jsonArray = new JSONArray();

			JSONObject jsonObj = new JSONObject();

			try {

				Collection<User> user = UserManager.getInstance()

						.findUsersByName(name);

				

                               jsonArray.addAll(user);
 

				jsonObj.put("jsonText", jsonArray);

			} catch (Exception e) {

				e.printStackTrace();

			}
 

			String jsonString = jsonObj.toString();

			System.out.println(jsonString);

Open in new window

0
Comment
Question by:Amos_Mike
  • 6
  • 4
12 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24822103
Perhaps add the new key with the value and remove the old ?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24822150
In JS:



<script>

 jsonText = [ { "description" : "Employee",

        "displayName" : "Guillaumin, Xavier",

        "email" : "xguillaumin@hersheys.com",

        "location" : "Carretera GDL-ElCastilloKM8.05",

        "title" : "Financial Coordinator Global Exports",

        "userId" : "HERXGG40"

      },

      { "description" : "Former Employee",

        "displayName" : "Lugo, Javier",

        "email" : "JLugo@Hersheys.com",

        "location" : "",

        "title" : "Special Projects Manager",

        "userId" : "HERJEL40"

      },

      { "description" : "Employee",

        "displayName" : "Plitnick, Xavier F",

        "email" : "HFXFP001@hersheys.com",

        "location" : "6 Scotch Pine Drive",

        "title" : "HZ_1526_Mst Wrap Mech",

        "userId" : "HFXFP001"

      }

    ] 

jsonText[0].text=jsonText[0].displayName

delete jsonText[0].displayName;

    

alert(jsonText[0].text)    

alert(jsonText[0].displayName)    

</script>

Open in new window

0
 

Author Comment

by:Amos_Mike
ID: 24822271
mplugjan

"jsonText" is a part of JSON object, it's a key in Json Object. I am sorry,  I cudn't figure out what are you trying to convey?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24822328
trying to convey you probably need to copy the key and delete the old.

Anyway why do you want to rename?
0
 

Author Comment

by:Amos_Mike
ID: 24823097
K got your point..
Thanks , but the change i am trying to do is on server side i.e.  in a servlet.
So i don't think this will help.

I am passing this JSON object to my javascript and there my jquery will read it for an JSONsuggest function.
my JS file for JSON suggest (plugin in jquery) expects the searchdata key to be "text", that's why i need to rename it from displayName to text
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24825672
And not easier to change the JS?
0
 

Author Comment

by:Amos_Mike
ID: 24825783
I know that could be the solution, but the thing is i am using that specific key from JS at other places in my application.
Moreover i was curious if there's a workaround, for what i have asked.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24826337
If it is not a huge amount of data, ADD the data to a new key
0
 

Author Comment

by:Amos_Mike
ID: 24826823
i got the solution, i sat and applied my brain's deepest nerves to get it working in JAVA servlet itself.
I am calling this mapBeans fxn, and replacing my fields from JSONArray.
this fxn references the UserMappingBean which has getter and setter for Text field.
It works fine for me.
Thanks for the help.
private Collection<UserMappingBean> mapBeans(Collection<User> users) {

		List<UserMappingBean> beans = new LinkedList<UserMappingBean>();

		

		for (User user: users) {

			UserMappingBean b = new UserMappingBean();

			b.setText(user.getDisplayName());

			beans.add(b);

		}

Open in new window

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 250 total points
ID: 24829119
"If it is not a huge amount of data, ADD the data to a new key"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sums of coloumns in html/java 15 66
jQuery Syntax 2 47
Reference Error with plugin 1 31
Creating a dynamic URL - set dates to upcoming weekend 6 31
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now