本文介绍了将字符串作为JSON对象发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个赋值,要求我把一个字符串作为一个json对象,在发送对象之前我必须将这个json对象放入一个http头。这是我的代码:

I have an assignment that requires me to put a string as a json object and before sending the object i have to put this json object into a http header. This is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *nid = @"";
    NSString *vocab = @"";
    NSString *inturl = @"testoverview";
    NSString *mail = @"[email protected]";
    NSString *md5pw = @"4d57e7ef1b7c3f431aca424764e9d786";

    NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                    nid, @"nid",
                                    vocab, @"vocab",
                                    inturl, @"inturl",
                                    mail, @"mail",
                                    md5pw, @"md5pw",nil];


    NSString *json = @"{nid:"",vocab:"",inturl:testoverview, mail:[email protected], md5pw:4d57e7ef1b7c3f431aca424764e9d786}";


    NSError *error;



    NSString *url = @"http://udv.taenk.dk/chh/drupal-taenk/services/mobile";


    NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];


    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:30.0];

     NSURLConnection *connction = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    FSRemoteExecutor *remote = [[FSRemoteExecutor alloc] init];
    [remote execute:url andHandler:[FSProductTestHandler alloc] JSONString:jsonString JSONData:jsonData Connection:connction];
    [remote connection:connction didReceiveData:jsonData];
    [remote connectionFinishedLoading:connction];

我的问题是我不能将jsonDictionary与对象一起使用并发送它,因为字符串格式服务必须收到的是:

My problem is that i can't use the jsonDictionary with objects and send it, cuz the string format that the service has to receive is this:

{nid:,vocab:,inturl:testoverview,mail :,md5pw:}

"{"nid":"","vocab":"", "inturl":"testoverview", "mail":"", "md5pw":""}"

字典会在字符串中插入=,这样我就无法得到服务的响应。

The dictionary would insert = in the string and that would give me no response from the service.

我想发送一个字符串(代码中的json)作为dataWithJSONObject:jso,喜欢这个:

I want to send a string (json in the code) as dataWithJSONObject:jso, likes this:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:&error];

但是我得到一个错误,因此:

but i get an error satating this:

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* + [NSJSONSerialization dataWithJSONObject:options:error:]:JSON写入中的顶级类型无效'

任何人都可以帮我吗?

推荐答案

但为什么要用使用 NSJSONSerialization 然后呢?
你做了你自己的JSON字符串( NSString * json = @{nid:,vocab:,inturl:testoverview,mail:chh @ fbr.dk,md5pw:4d57e7ef1b7c3f431aca424764e9d786} ; )。然后你不需要再次运行 NSJSONSerialization

But why use use NSJSONSerialization then?You did your own JSON string (NSString *json = @"{nid:"",vocab:"",inturl:testoverview, mail:[email protected], md5pw:4d57e7ef1b7c3f431aca424764e9d786}";). Then you don't need to run again a NSJSONSerialization.

只需从<$ c中生成一个NSData $ c> * json(NSString)然后将其发送到服务器。

Just make a NSData out of your *json (NSString) and then send it to the server.

使你的NSData像这样:

Make your NSData like this:

NSData *jsonPayload = [json dataUsingEncoding:NSUTF8StringEncoding];

这篇关于将字符串作为JSON对象发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:33