type java.lang.String cannot be converted to JSONObject

I have a java String variable newTempTT which holds the following:

myObject = {"VehicleID":"KL-9876","VehicleType":"Nissan","VehicleOwner":"Sanjiva"}

I would like to add this into a json object so i performed the following line of code:

JSONObject jObject = new JSONObject(newTempTT);

There is no error message but when i run the program it says

"of type java.lang.String cannot be converted to JSONObject"

What changes do I need to make in order to convert this string to a JSONObject. Thanks in advance.

Answers


Include escape characters like this for every double quotes.

 String newTempTT = "myObject = {\"VehicleID\":\"KL-9876\",\"VehicleType\":\"Nissan\",\"VehicleOwner\":\"Sanjiva\"}";

JSONObject jObject = new JSONObject(newTempTT);

From the javadoc:

    Parameters: source - `A string beginning with { (left brace) and ending with } (right brace).` 
    Throws:
    JSONException - If there is a syntax error in the source string or a duplicated key.

Check if you are following this currently.


You seem to be encoding things to JSON yourself. Don't do this, let the library do it for you.


Need Your Help

Visual Studio 2003 Cant use pdb file in both debug mode and release mode

c++ debugging visual-studio-2003 pdb

I have a Visual Studio 2003 installed on a Windows7-64Bit. When I build a project in vs2003 it creates a pdb file. For example: for a project named SampleProject, it creates a SampleProject.pdb and...

UISegmentedControl embedded in a UINavigationBar/Item

ipad ios uisegmentedcontrol uinavigationitem

I would like to embed a UISegmentedControl somewhere in my UINavigationControllers topbar.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.