Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8610477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:08:21+00:00 2026-06-12T04:08:21+00:00

Hi guys I’m developing a Restful + JSON servlet, but I got a strange

  • 0

Hi guys I’m developing a Restful + JSON servlet, but I got a strange problem, if i try to compile my project source with this method:

public
static
WipdDBTable
parseJSON(String JSONBody)
{
    JSONObject jsonObj;
    JSONTokener jsonTok;
    Iterator it;
    String[] labels;
    String[][] fields;
    int i;

    try  {
        jsonTok = new JSONTokener(JSONBody);
        jsonObj = new JSONObject(jsonTok);

        labels = new String[jsonObj.length()];
        fields = new String[1][labels.length];

        i = 0;
        it = jsonObj.keys(); 
        while(it.hasNext())  {
            String key = it.next().toString();
            labels[i] = key;
            fields[0][i] = jsonObj.get(key);
            i++;
        }

        return new WipdDBTable(labels, fields);
    } catch(JSONException ex)  {
        return null;
    }
}

I get this error:

WipdJSON.java:102: incompatible types
found   : java.lang.Object
required: java.lang.String
                fields[0][i] = jsonObj.get(key);

So I wrote a test class, with apparently the same source, but with this one I don’t get any error:

public class jsontest
{
    public static
    void
    main(String[] args)
    {
        String s1;
        JSONObject jsonObj;
        JSONTokener jsonTok;

        s1 = "{\"lo\":\"ol\",\"json\":{\"1\":\"2\"},\"yo\":{\"lol\":\"lol\"}}";

        try  {
            jsonTok = new JSONTokener(s1);
            jsonObj = new JSONObject(jsonTok);

            Iterator it = jsonObj.keys();
            while(it.hasNext())  {
                String key = it.next().toString();
                System.out.print(key + "=>");
                System.out.println(jsonObj.get(key));
            }
        } catch(JSONException ex)  {
            ex.printStackTrace();
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:08:23+00:00Added an answer on June 12, 2026 at 4:08 am

    jsonObj.get(key); return an Object, you need to cast it to a String if you want to get the value in a String variable. There is a getString method in the JSONObject class:

    fields[0][i] = jsonObj.getString(key);
    

    And you don’t get any error in your test class as you’re only outputting the value that is calling the toString() method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

guys. I have a strange problem. I try to write unit-tests to web-app. I
Guys i need someone fix this problem ? when i compile that code i
Guys, I've came across this problem I can't resolve myselg, I'm pretty sure I
Guys i've got an array thats like this: array(3) { [2]=> array(1) { [name]=>
Guys, this probably will be fairly simple, but how am I able to find
guys! I have problem with my .net application. When I try to print in
Guys I've 3 tables as follows: Project {id, name, owner, ....} ProjectMilestones {id, project_id,
guys! I've got 2 forms in application - working form (frmMain) and form of
Guys, Im calling a JsonResult but the getJSON is sending a null parameter to
guys, I have the problem when copying database from local assets folder to /data/data/package_name/databases

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.